题目 没有用链表,感觉用指针写太麻烦了,想起当初刚学数据结构被链表支配的恐惧。坑点是可能的有不在链表的数据,链表的长度不一定为n.
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
int judge1[N], judge2[N], v[N];
int main()
{
int st, n, k, a, b, c;
scanf("%d%d%d", &st, &n, &k);
for(int i=0; i<n; i++) {
scanf("%d%d%d", &a, &b, &c);
judge1[a]=b;
judge2[a]=c;
}
int i=0;
n=0;
while(st!=-1) {
v[i++]=st;
st=judge2[st];
n+=1;
}
int j=n/k;
if(n%k)
i=n-n%k;
else {
i=n-k;
j-=1;
}
while(true){
printf("%05d %d ", v[i], judge1[v[i]]);
i+=1;
if(i==n || i%k==0){
j-=1;
if(j<0)
break;
i=j*k;
}
printf("%05d\n", v[i]);
}
printf("-1\n");
return 0;
}
|