1025. 反转链表 (25)PAT乙级&&1074. Reversing Linked List (25)PAT甲级

甲级传送门
乙级传送门

#include
#include


using namespace std;

#define MAX_N 100100

struct Node{
    int address;
    int next;
    int data;
    int order;
}node[MAX_N];

bool cmp(struct Node a,struct Node b){
    return a.orderint main(){
    for(int i=0;iint begin;
    int n;
    int k;
    scanf("%d%d%d",&begin,&n,&k);
    int address;
    for(int i=0;i"%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    int count=0;
    for(int p=begin;p!=-1;p=node[p].next){
        node[p].order=count++;
    }
    sort(node,node+MAX_N,cmp);
    n=count;
    for(int i=0;ifor(int j=(i+1)*k-1;j>i*k;j--){
            printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);
        }
        printf("%05d %d ",node[i*k].address,node[i*k].data);
        if(i1){
            printf("%05d\n",node[(i+2)*k-1].address);
        }
        else{
            if(n%k==0){
                printf("-1\n");
            }
            else{
                printf("%05d\n",node[(i+1)*k].address);
                for(int l=n/k*k;lprintf("%05d %d ",node[l].address,node[l].data);
                    if(l!=n-1)
                        printf("%05d\n",node[l+1].address);
                    else
                        printf("-1\n");
                }


            }

        }

    }

}

你可能感兴趣的:(PAT,甲级,PAT,乙级)