题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805296180871168
这道题不是很难,写了一会,主要是没理解清楚题意,机试的时候一定要多多注意!!
重点是42-44行反转那里,其他都是题意没理解懂,模版题
代码
#include<vector>#include<algorithm>#include<iostream>#include<cstdio>#include<math.h>using namespace std;const int maxn = 100010;struct Node{int address, data, next;bool flag;int order;}node[maxn];bool cmp(Node a, Node b){if(a.flag == false || b.flag == false) return a.flag > b.flag;return a.order < b.order;}int main(){for(int i = 0; i < maxn; i++){node[i].flag = false;node[i].order = maxn;}int begin, n, k;scanf("%d%d%d", &begin, &n, &k);int address, data, next;for(int i = 0; i < n; i++){scanf("%d%d%d", &address, &data, &next);node[address].address = address;node[address].data = data;node[address].next = next;}int p = begin, count = 0;while(p != -1){node[p].order = count;node[p].flag = true;p = node[p].next;count++;}sort(node, node + maxn, cmp);for(int i = 0; i < count / k ; i++){//printf("i = %d\n",i);reverse(node + i * k, node + (i + 1) * k);}//if(count % 4 == 0) reverse(node + count - 4, node + count);for(int i = 0; i < count; i++){if(i != count - 1) printf("%05d %d %05d\n", node[i].address, node[i].data, node[i + 1].address);else printf("%05d %d -1\n", node[i].address, node[i].data);}return 0;}
