zoukankan      html  css  js  c++  java
  • PAT 甲级 1052 Linked List Sorting

    https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive N (<) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −.

    Then N lines follow, each describes a node in the format:

    Address Key Next
    

    where Address is the address of the node in memory, Key is an integer in [−], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

    Output Specification:

    For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

    Sample Input:

    5 00001
    11111 100 -1
    00001 0 22222
    33333 100000 11111
    12345 -1 33333
    22222 1000 12345
    

    Sample Output:

    5 12345
    12345 -1 00001
    00001 0 11111
    11111 100 22222
    22222 1000 33333
    33333 100000 -1

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1e5 + 10;
    int N, St;
    
    struct Node {
        int st;
        int key;
        int en;
    }node[maxn], t;
    
    vector<Node> num;
    
    bool cmp(const Node& a, const Node& b) {
        return a.key < b.key;
    }
    
    int main() {
        scanf("%d%d", &N, &St);
        for(int i = 1; i <= N; i ++) {
            scanf("%d%d%d", &t.st, &t.key, &t.en);
            node[t.st].st = t.st;
            node[t.st].key = t.key;
            node[t.st].en = t.en;
        }
    
        while(St != -1) {
            num.push_back(node[St]);
            St = node[St].en;
        }
    
        if(num.size() == 0) {
            printf("0 -1
    ");
            return 0;
        }
    
        sort(num.begin(), num.end(), cmp);
    
        printf("%d %05d
    ", num.size(), num[0].st);
        for(int i = 0; i <num.size() - 1; i ++) {
            printf("%05d %d %05d
    ", num[i].st, num[i].key, num[i + 1].st);
        }
        printf("%05d %d -1
    ", num[num.size() - 1].st, num[num.size() - 1].key);
        return 0;
    }
    

      

  • 相关阅读:
    pycharm快捷键、常用设置、包管理
    【转载】TCP socket心跳包示例程序
    【转载】C++定时器实现
    金龙一卡通登陆爬取数据 免验证码 多线程 学生卡 CAUC
    python3 正方教务系统 爬取数据
    29、Python之Web框架Django入门
    28、Python之前端组件学习
    27、Python之jQuery基础
    26、Python之JavaScript进阶篇
    25、Python之JavaScript基础
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9852900.html
Copyright © 2011-2022 走看看