1.没看到reverse the links of every K elements on L这一句,WA到死。
2.代码:
#include<iostream> #include<cstring> #include <algorithm> #include<cstdio> using namespace std; struct Node { int d; int next; void init(int x,int y) { d=x; next=y; } }; Node a[100005]; int Hash[100005]; int main() { int addr,n,k; scanf("%d%d%d",&addr,&n,&k); for (int i = 0; i<n; i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); a[x].init(y,z); } int pos = 0; int next= addr; while (next!= -1) { Hash[pos++] = next; next = a[next].next; } int i=0; while (i + k <= pos)//reverse the links of every K elements on L { reverse(Hash+i, Hash+i+ k);//将区间[i,i+k-1]里的值倒过来 i += k; } for (i = 0; i < pos - 1; i++) { printf("%05d %d %05d\n", Hash[i], a[Hash[i]].d, Hash[i+1]); } printf("%05d %d -1\n", Hash[i], a[Hash[i]].d); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-10 08:22:17