pat甲级1074. Reversing Linked List (25)、乙级1025. 反转链表 (25)
2018-03-10 15:15
483 查看
甲级1074. Reversing Linked List (25)
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.Input Specification:Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.Then N lines follow, each describes a node in the format:Address Data Nextwhere Address is the position of the node, Data is an integer, and Next is the position of the next node.Output Specification:For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.Sample Input:
内存限制65536 kB
代码长度限制8000 B
判题程序Standard作者CHEN, Yue
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。输入格式:每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。接下来有N行,每行格式为:Address Data Next其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。输出格式:对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。输入样例:
(2)由于所给的地址是5位非负整数,可以定义一个长度为10的6次方大的数组node,以数组下标表示地址,数组元素为结点类对象。
(3)定义一个长度为N(结点总数)的int数组order,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到order数组中。
(4)按要求对order数组进行翻转,可以利用c语言库里的reverse函数
(5)按格式要求进行结果输出
(2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转
(3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出
时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.Input Specification:Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.Then N lines follow, each describes a node in the format:Address Data Nextwhere Address is the position of the node, Data is an integer, and Next is the position of the next node.Output Specification:For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.Sample Input:
00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218Sample Output:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
乙级1025. 反转链表 (25)
时间限制300 ms内存限制65536 kB
代码长度限制8000 B
判题程序Standard作者CHEN, Yue
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。输入格式:每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。接下来有N行,每行格式为:Address Data Next其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。输出格式:对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。输入样例:
00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218输出样例:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
算法设计:
(1)定义结点类,其中包含两个域:数据域和下一结点的地址域。(2)由于所给的地址是5位非负整数,可以定义一个长度为10的6次方大的数组node,以数组下标表示地址,数组元素为结点类对象。
(3)定义一个长度为N(结点总数)的int数组order,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到order数组中。
(4)按要求对order数组进行翻转,可以利用c语言库里的reverse函数
(5)按格式要求进行结果输出
注意点:
(1)题目给出的结点中可能有不在链表中的无效结点,所以需要另外一个变量来表示链表中有效结点的个数(2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转
(3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出
c++代码:
#include<bits/stdc++.h> using namespace std; struct Node{ int data; int next; }; int main(){ int begin,N,K; scanf("%d%d%d",&begin,&N,&K); Node node[100005];//定义结点数组,数组长度至少为10的6次方大 //读取结点内容 for(int i=0;i<N;++i){ int address; scanf("%d",&address); scanf("%d%d",&node[address].data,&node[address].next); } int order ={0};//定义储存链表各结点地址的order数组 int count=0;//表示链表中有效结点的个数 //读取有效结点的地址到order数组 while(begin!=-1){ order[count++]=begin; begin=node[begin].next; } //按要求翻转order数组,注意为满足N%K==0的情况,循环跳出条件需为i<=count而不能是i<count for(int i=K;i<=count;i+=K){ reverse(order+i-K,order+i); } //输出结果 for(int i=0;i<count;++i){ printf("%05d %d ",order[i],node[order[i]].data); if(i==count-1) printf("-1");//对地址-1进行特判 else printf("%05d\n",order[i+1]); } return 0; }
相关文章推荐
- 1025. 反转链表 (25)PAT乙级&&1074. Reversing Linked List (25)PAT甲级
- PAT - 甲级 - 1074. Reversing Linked List (25)(链表)
- PAT 1074. Reversing Linked List (25)(链表反转)
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
- PAT甲级1074. Reversing Linked List (25)
- PAT乙级 1025. 反转链表 (25)--结构体、容器vector、容器pair
- PAT乙级真题及训练题 1025. 反转链表 (25)
- 1025. 反转链表 (25) PAT乙级真题
- [PAT乙级]1025. 反转链表 (25)
- 1025. 反转链表 (25)--PAT乙级
- PAT乙级 1025. 反转链表 (25)
- 乙级 PAT 1025. 反转链表 (25)
- 1074. Reversing Linked List (25)-PAT甲级真题
- PAT-乙级-1025. 反转链表 (25)
- PAT乙级1025. 反转链表 (25)
- 1025. 反转链表 (25)-浙大PAT乙级真题java实现
- PAT乙级 1025. 反转链表 (25)
- PAT-乙级-1025. 反转链表 (25)
- PAT乙级真题及训练集(20)--1025. 反转链表 (25)
- PAT 甲级 1074. Reversing Linked List (25)