• 1052 Linked List Sorting (25 分)


    1052 Linked List Sorting (25 分)
     

    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

    是在下蠢了,写错了一个变量,调了半个小时(哭晕在厕所)。


     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Node
     6 {
     7     string s, ends;
     8     int val;
     9     friend bool operator < (const Node &a, const Node &b){
    10         return a.val < b.val;
    11     }
    12 }node[100005],ans[100005];
    13 map<string, int> mp;
    14 int n;
    15 string ss;
    16 int main(){
    17     cin >> n >> ss;
    18     for(int i = 0; i < n; i++){
    19         cin >> node[i].s >> node[i].val >> node[i].ends;
    20         mp[node[i].s] = i;
    21     }
    22     int p = 0;
    23     while(ss != "-1"){
    24         int a = mp[ss];
    25         ans[p++] = node[a];
    26         ss = node[a].ends;
    27     }
    28     if(p == 0){
    29         cout <<"0 -1"<<endl;
    30         return 0;
    31     }
    32     sort(ans, ans+p);
    33     cout << p << " " << ans[0].s << endl;
    34     for(int i = 0; i < p-1; i++){
    35         cout << ans[i].s <<" "<<ans[i].val << " "<<ans[i+1].s<<endl;
    36     }
    37     cout << ans[p-1].s <<" "<<ans[p-1].val << " "<<"-1"<<endl;
    38     return 0;
    39 }


  • 相关阅读:
    两次动态输入和while的结合使用
    索引切片步长
    12.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
    输出1-100的所以奇数或偶数
    求1-2+3-4+5 ... 99的所有数的和
    求1-100所有数的和
    三次登录机会
    while输入12345689
    while和格式化输出的复合使用
    44
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11185665.html
Copyright © 2020-2023  润新知