思路:因为要进行反转,所以用C++中的reverse()函数简单点,用数组进行存储
代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int data[100005], next[100005],list[100005];
int first, tmp, n, k;
cin >> first >> n >> k;
int i;
for (i = 0; i < n; i++)
{
cin >> tmp;
cin >> data[tmp] >> next[tmp];
}
int sum = 0;
while (first != -1)
{
list[sum++] = first;
first = next[first];
}
for (i = 0; i < sum-sum%k; i += k)
reverse(begin(list) + i, begin(list) + i + k);
for (i = 0; i < sum-1; i++)
printf("%05d %d %05d\n", list[i], data[list[i]], list[i+1]);
printf("%05d %d -1\n", list[sum - 1], data[list[sum - 1]]);
return 0;
}