标签:max scanf fir must ota nes math position ret
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.
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤) 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 Next
where Address
is the position of the node, Data
is an integer, and Next
is the position of the next node.
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.
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 // A1074.cpp 2 // 3 #include <stdio.h> 4 const int MAX = 100005; 5 int Data[MAX], Next[MAX],List[MAX]; 6 7 int main() 8 { 9 int FirAdd, n, k; 10 scanf("%d%d%d", &FirAdd, &n, &k); 11 for (int i = 0; i < n; i++) { 12 int tmpAdd, tmpData, tmpNext; 13 scanf("%d%d%d", &tmpAdd, &tmpData, &tmpNext); 14 Data[tmpAdd] = tmpData; 15 Next[tmpAdd] = tmpNext; 16 } 17 //从FirAddres开始将所有结点地址串起来 18 int idx = 0; 19 while (FirAdd != -1) { 20 List[idx++] = FirAdd; 21 FirAdd = Next[FirAdd]; 22 } 23 //将串好的地址List进行分组翻转 24 for (int i = 0; i<idx-idx%k; i += k) { 25 //对称性翻转 26 for (int j = 0; j < k / 2;j++) { 27 int tmp = List[i + j]; 28 List[i + j] = List[i + k -j- 1]; 29 List[i + k -j- 1] = tmp; 30 } 31 } 32 //输出 33 //printf("\n"); 34 for (int i = 0; i < idx-1; i++) { 35 printf("%05d %d %05d\n", List[i], Data[List[i]], List[i+1]); 36 } 37 //最后一个结点 38 printf("%05d %d -1\n", List[idx-1], Data[List[idx-1]]); 39 return 0; 40 }
标签:max scanf fir must ota nes math position ret
原文地址:https://www.cnblogs.com/PennyXia/p/12485581.html