根据评论里的那种思路写的一个代码。这种思路很简单,用一个数组保存每个节点的地址,然后对数组进行reverse。这个题目还可以运用map,也非常方便。
#include<iostream> #include<cstring> #include <algorithm> using namespace std; int node[100002][2]; int main() { int list[100002]; int i; int st, k; int n; cin >> st >> n >> k; for (i = 0; i<n; i++) { int add, data, next; cin >> add >> data >> next; node[add][0] = data; node[add][1] = next; } int cnt = 0; int cur = st; while (cur != -1) { list[cnt++] = cur; cur = node[cur][1]; } i = 0; while (i + k <= cnt) { reverse(list + i, list + i + k); i += k; } for (i = 0; i < cnt - 1; i++) { printf("%05d %d %05d\n", list[i], node[list[i]][0], list[i+1]); } printf("%05d %d -1\n", list[i], node[list[i]][0]); return 0; }
之前写了一个代码,在最底下,有bug,最后一个1分的case无法通过,原因是没有考虑到输入的N个节点,有某个或某些不在链表上,所以最后要输出的总的节点数小于N。
这个题目我用的方法非常蛋疼,写了很久。这个题目还有很多取巧的办法。我完整通过的代码如下:
#include <iostream> #include <vector> using namespace std; struct Node { int data, next; }node[100005]; void printList(int start) { int cur = start; while (node[cur].next != -1) { printf("%05d %d %05d\n", cur, node[cur].data, node[cur].next); cur = node[cur].next; } if (node[cur].next == -1) { printf("%05d %d -1\n", cur, node[cur].data); } } int reverseK(int start, int n, int k) { int head, pre, cur, next, pos, len1, len2, times = n / k; vector<int> first, last; first.push_back(start); for (int i = 0; i < times; i++) { pre = -1; cur = start; for (pos = 1; pos <= k; pos++) { next = node[cur].next; if (pos == k) { if (i == 0) head = cur; last.push_back(cur); first.push_back(next); } node[cur].next = pre; pre = cur; cur = next; } start = next; } len1 = first.size(), len2 = last.size(); for (int i = 0; i < len2 - 1; i++) node[first[i]].next = last[i + 1]; if (len1 > len2) node[first[len1 - 2]].next = first[len1 - 1]; return head; } int main() { int head, N, K; cin >> head >> N >> K; int len = 1; int Address, Data, Next; for (int i = 0; i < N; i++) { cin >> Address >> Data >> Next; node[Address].data = Data; node[Address].next = Next; } int cur = head; while (node[cur].next != -1) { len++; cur = node[cur].next; } if (K != 1) head = reverseK(head, len, K); printList(head); return 0; }
before:
方法非常蛋疼,最后一个1分的case始终过不了,蛋疼。代码如下,等待改进。。。
ps:如果你发现了我代码中的bug,为什么过不了,以及如果你有更好的方法,请一定要留言告诉我,非常感谢!
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> using namespace std; struct Node { int data, next; }node[100005]; void printList(int start, int n) { int cur = start; for (int i = 0; i < n; i++) { printf((node[cur].next == -1 ? "%05d %d %d\n" : "%05d %d %05d\n"), cur, node[cur].data, node[cur].next); cur = node[cur].next; } } int reverseK(int start, int n, int k) { int head, pre, cur, next, pos, len1, len2, times = n / k; vector<int> first, last; first.push_back(start); for (int i = 0; i < times; i++) { pre = -1; cur = start; for (pos = 1; pos <= k; pos++) { next = node[cur].next; if (pos == k) { if (i == 0) head = cur; last.push_back(cur); first.push_back(next); } node[cur].next = pre; pre = cur; cur = next; } start = next; } len1 = first.size(), len2 = last.size(); for (int i = 0; i < len2 - 1; i++) node[first[i]].next = last[i + 1]; if (len1 > len2) node[first[len1 - 2]].next = first[len1 - 1]; return head; } int main() { // freopen("in.txt", "r", stdin); int head, N, K; cin >> head >> N >> K; int Address, Data, Next; for (int i = 0; i < N; i++) { cin >> Address >> Data >> Next; node[Address].data = Data; node[Address].next = Next; } if (K != 1) head = reverseK(head, N, K); printList(head, N); return 0; }