PAT A 1052 Linked List Sorting(钻空子一时爽 一直钻一直爽)

版权声明:本文为博主原创文章,欢迎交流学习,未经博主允许不得转载。 https://blog.csdn.net/qq_43749739/article/details/89716311

注:备战2020ing…暂时没时间注解啦,大家凑合先看下代码叭,肥宅对不住了。

#include <cstdio>
#include <algorithm>
using namespace std;
typedef struct
{    int k, next, addr;    }node;
int cmp(node a, node b)
{    return a.k < b.k;   }
int main()
{
    node L[100000];
    int N, Head, Ad, cnt = 0;
    scanf("%d %d", &N, &Head);
    if( Head == -1 )
    {   printf("0 -1");    return 0;    }
    for( int i = 0; i < N; ++i )
    {
        scanf("%d", &Ad);
        scanf("%d %d", &L[Ad].k, &L[Ad].next);
        L[Ad].addr = Ad;
    }
    node A[N];
    for( int i = Head; i != -1; i = L[i].next, ++cnt )
        A[cnt] = L[i];
    sort(A, A + cnt, cmp);
    printf("%d ", cnt);
    for(int i = 0; i < cnt; ++i)
        printf("%05d\n%05d %d %s", A[i].addr, A[i].addr, A[i].k, i == cnt - 1 ?"-1":"");
}

猜你喜欢

转载自blog.csdn.net/qq_43749739/article/details/89716311