題目:
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 Next
where 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 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
測驗結果:

#include<iostream>
#include<iomanip>
using namespace std;
class Node
{
public:
int thisindex = -1;
int next = -1;
int pre = -1;
int val;
};
int main()
{
Node* link = new Node[100005];
int head, n, k;
cin >> head >> n >> k;
int i;
for (i = 0; i < n; i++) {
int index, val, next;
cin >> index >> val >> next;
link[index].thisindex = index;
link[index].val = val;
link[index].next = next;
link[next].pre = index;
}
int kindex = head;
while (k - 1) {
kindex = link[kindex].next;
k--;
}
int p = kindex;
while (p != -1) {
cout << setfill('0') << setw(5) << link[p].thisindex << " " << link[p].val << " ";
if (link[p].pre == -1) {
if (link[kindex].next == -1) {
cout << link[kindex].next << "\n";
}
else {
cout << setfill('0') << setw(5) << link[kindex].next << "\n";
}
}
else {
cout << setfill('0') << setw(5) <<link[p].pre << "\n";
}
p = link[p].pre;
}
p = link[kindex].next;
while (p != -1) {
cout << setfill('0') << setw(5) << link[p].thisindex << " " << link[p].val << " ";
if (link[p].next == -1) {
cout << link[p].next << "\n";
}
else {
cout << setfill('0') << setw(5) << link[p].next << "\n";
}
p = link[p].next;
}
system("pause");
return 0;
}
uj5u.com熱心網友回復:
邊界條件輸入輸出格式
……
uj5u.com熱心網友回復:
我也是這兩個測驗點通不過。。。uj5u.com熱心網友回復:
你好,請問pat測驗點的資料哪里有啊uj5u.com熱心網友回復:
全是英文,看不懂啊。。。。。。。。。。。。。uj5u.com熱心網友回復:
這道題的題意是每k位翻轉一次,不是前k位只翻轉一次。第一個題中給的例子是3->2->1->6->5->4而不是3->2->1->4->5->6uj5u.com熱心網友回復:
樓上正解,大哥,你連題目中給出的測驗樣例都沒過......你試試00100 6 3
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113109.html
標籤:C++ 語言
