p267 链表处理 B1025 todo??

//p267 链表处理 B1025  todo//clion  为什么不Build就可以直接Run出新结果??
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
struct Node{
    int address,data,next;
    int order;//结点在链表上的序号,无效节点记为maxn

}node[maxn];

bool cmp(Node a,Node b){
    return a.order < b.order;
}
int main(){
    for(int i=0;i<maxn;i++){
        node[i].order = maxn;
    }

    int begin,n,K,address;
    scanf("%d%d%d",&begin,&n,&K);
    for(int i=0;i<n;++i){
        scanf("%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address = address;
    }

    int p = begin,count = 0;
    while(p != -1){
        node[p].order = count++;
        p = node[p].next;

    }
    sort(node,node + maxn,cmp);
    n = count;
    for(int i=0;i<n/K;i++){
        for(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(i<n/K-1){
            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 i=n/K*K;i<n;i++){
                    printf("%05d %d ",node[i].address,node[i].data);
                    if(i<n-1){
                        printf("%05d\n",node[i+1].address);
                    }else{
                        printf("-1\n");
                    }
                }
            }
        }
    }

    return 0;
}

全部评论

相关推荐

曲霜晨:娱乐至上的年代
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务