PTA A_1074 Reversing Linked List
传送门:https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144解:
链表+排序
#include<cstdio>
#include<algorithm>
using namespace std;
typedef struct Node{
int add, data, next, key;
}node;
node a;
bool cmp(node a, node b)
{
return a.key < b.key;
}
int main(void)
{
int head, n, k;
scanf("%d %d %d", &head, &n, &k);
for (int i = 0; i < n; i++)
{
int add;
scanf("%d", &add);
scanf("%d %d", &a.data, &a.next);
a.add = add;
}
int count = 0, j = -n;
for (int i = head; i != -1; i = a.next, count++)
a.key = j++;
sort(a, a + 100010, cmp);
for (int i = 0; i < count / k; i++)
reverse(a + i * k, a + (i + 1) * k);
for (int i = 0; i < count - 1; i++)
printf("%05d %d %05d\n", a.add, a.data, a.add);
printf("%05d %d -1", a.add, a.data);
return 0;
}
页:
[1]