Home > Back-end >  Consulting about PAT serie b 1025 part of the answer wrong
Consulting about PAT serie b 1025 part of the answer wrong

Time:09-28

1025. The inversion list (25)
Given a constant K and a singly linked list L, please write a program to L in each reverse K nodes, such as: given the L for 1-2-3-4-5-6, K is 3, the output should be 3-2-1-6-5 -> 4; If is 4 K, the output should be 4-3-2-1-5-6, last less than K element does not reverse,

Input format:

Each input contains one test case, each test case line 1 give the address of the first node, the total number of positive integer N (& lt;=105), as well as the positive integer K (& lt;=N), which requires inversion of sub chain node number, the address of the node is five nonnegative integers, NULL address expressed with 1,

The next N lines, each line format for:

The Address Data Next

The Address is a node Address, the Data is saved the nodes integer Data, Next is the Address of the Next node,

The output format:

Sequence of each test case, output the list after inversion, each node of a line, its format is the same as the input,

Input the sample:
00100 June 4
00000 4 99999
00100 1 12309
68237 6-1
33218 3 00000
99999 5 68237
12309 2 33218
The output sample:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6-1

Here is my code:

 
# include
using namespace std;
Struct Node {
Int AD;
The int data;
Int next;
} node [100001], \ [100001];
Int main ()
{
Int fstad, n, k;
While (the scanf (" % d % d % d ", & amp; Fstad, & amp; N, & amp; K)!=(EOF) {//input starting address, number of nodes and exchange length
for(int i=0; IThe scanf (" % d % d % d ", & amp; The node [I]. AD, & amp; The node [I]. The data, & amp; The node [I] next);//the address of the input node, the node number and point to the next node address
}
Int j=0;//statistical effective number of node
for(int i=0; IIf (fstad==node [I] AD) {
\ [j++]=node [I];
Fstad=node [I] next;
I=1;
}
If (fstad==1) break;
}
Int cont=0;
Int t=0;
For (int I=k - 1; I>=0 & amp; & ContNode [t++]=\ [I];
If (I==cont * k) {
Cont++;
I=cont * k + k;
}
}
If (j % k! Enough K=0) {//behind the part directly into the corresponding node in the []
For (int I=j - j % k; INode [I]=\ [I];
}
}
for(int i=0; IThe node [I] next=node [I + 1] AD;
}
for(int i=0; IIf (node [I] next==1) printf (" % 5 d % d % d \ n ", the node [I] AD, node [I] data, node [I] next);
The else printf (" % 5 d % d % 5 d \ n ", the node [I] AD, node [I] data, node [I] next);
}
}
return 0;
}


I tested several examples, can not have a problem, but OJ judgment is the answer error and timeout, help everybody to help, thank you,

CodePudding user response:

@ confident boy can help have a look, thank you

CodePudding user response:

"If it is 4 K output should be 4-3-2-1-5-6"
If this sentence, K is 4, the output should be: 4-3-2-1-6 to 5

CodePudding user response:

@ confident boy subject to output the 4-3-2-1-5-6, also is the final can not meet the needs of K to transpose

CodePudding user response:

I also am the mistake, the building Lord found the problem?
  • Related