• PAT甲题题解-1052. Linked List Sorting (25)-排序


    三个注意点:

    1.给出的n个节点并不一定都在链表中

    2.最后一组样例首地址即为-1

    3.输出地址的时候一直忘记前面要补0。。。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string.h>
    using namespace std;
    const int maxn=100000+5;
    struct Node{
        int addr;
        int val;
        int to;
        bool operator<(const Node tmp)const{
            return val<tmp.val;
        }
    }linked[maxn],node[maxn];
    int main()
    {
        int n,first;
        int a,b,c;
        scanf("%d %d",&n,&first);
        for(int i=0;i<n;i++){
            scanf("%d %d %d",&a,&b,&c);
            linked[a].addr=a;
            linked[a].val=b;
            linked[a].to=c;
        }
        int cnt=0;
        while(first!=-1){
            node[cnt].addr=linked[first].addr;
            node[cnt].val=linked[first].val;
            node[cnt].to=linked[first].to;
            cnt++;
            first=linked[first].to;
        }
        sort(node,node+cnt);
        if(cnt==0){
            printf("0 -1
    ");  //最后一个样例有首地址为-1的情况。。。
            return 0;
        }
        printf("%d %05d
    ",cnt,node[0].addr);
        for(int i=0;i<cnt;i++){
            printf("%05d %d ",node[i].addr,node[i].val);
            if(i==cnt-1)
                printf("-1
    ");
            else
                printf("%05d
    ",node[i+1].addr);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    TCP通信丢包原因总结
    根据日志查看QPS
    mysql:备份、复制
    集群
    redis性能提升
    redis源码——多机数据库的实现
    redis源码——单机数据库的实现
    redis 设置过期Key 的 maxmemory-policy 六种方式
    字符处理
    贝塞尔曲线
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/6538799.html
Copyright © 2020-2023  润新知