PAT 1041 Linked List Sorting (25)(链表排序)

原创 2016年08月30日 09:00:47

题目

1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 (< 105) 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 -1.

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 [-105, 105], 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.先根据头地址保存这个链表所有有效的队列,然后在排序就行了。
  • 2.输出的时候要注意保存的链表为空的情况。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int nx[99999],va[99999];
struct node{
    int ad,v;
    node(){}
    node(int _ad,int _v){
        this->ad = _ad;
        this->v = _v;
    }
};
bool cmp(const node &a, const node &b){
    return a.v<b.v;
}

int main(int argc, char *argv[])
{
    int n,f;
    scanf("%d %d",&n,&f);
    vector<node> b;
    int tem_a,tem_b,tem_c;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d %d",&tem_a,&tem_b,&tem_c);
        nx[tem_a] = tem_c;
        va[tem_a] = tem_b;
    }
    //将有效的链表存好
    int now = f;
    while (now != -1) {
        b.push_back(node(now,va[now]));
        now = nx[now];
    }
    //输出
    if (!b.empty()) {
        sort(b.begin(),b.end(),cmp);
        printf("%d %05d\n",b.size(),b[0].ad);
        for (int i = 0; i < b.size()-1; ++i) {
            printf("%05d %d %05d\n",b[i].ad,b[i].v,b[i+1].ad);
        }
        printf("%05d %d %d\n",b[b.size()-1].ad,b[b.size()-1].v,-1);
    }else {
        printf("%d %d\n",0,-1);
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

1052. Linked List Sorting (25)【链表+排序】——PAT (Advanced Level) Practise

题目信息1052. Linked List Sorting (25)时间限制400 ms 内存限制65536 kB 代码长度限制16000 BA linked list consists of a...

PAT (Advanced Level) 1074. Reversing Linked List (25) 翻转链表

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem...

1074. Reversing Linked List (25)【链表翻转】——PAT (Advanced Level) Practise

题目信息1074. Reversing Linked List (25)时间限制400 ms 内存限制65536 kB 代码长度限制16000 BGiven a constant K and a ...

PAT (Advanced Level) 1097. Deduplication on a Linked List (25) 链表去重

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated...

PAT 1052. Linked List Sorting (25)

//1052. Linked List Sorting (25) //第三个case测试:输出格式是否为05d,尤其是输出起始节点。 //最后一个case是测试当链表node数量为0时,是否输出正...
  • xsloop
  • xsloop
  • 2015年03月16日 08:35
  • 182

【PAT】1052. Linked List Sorting (25)

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052 题目描述: A linked list consists of a series o...

浙江大学PAT_甲级_1052. Linked List Sorting (25)

题目链接:点击打开链接 A linked list consists of a series of structures, which are not necessarily adjac...

1052. Linked List Sorting (25)-PAT甲级真题

1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not ...
  • liuchuo
  • liuchuo
  • 2016年08月07日 20:10
  • 144

pat1052Linked List Sorting (25)

题意分析: (1)给出内存中链表的节点,节点定义为:address value nextaddress并且给出链表的起始地址,按照原来的格式输出这个链表经过按照value排序后的链表,即:链表节点的...

PAT甲级真题及训练集(17)--1052. Linked List Sorting (25)

1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Stan...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PAT 1041 Linked List Sorting (25)(链表排序)
举报原因:
原因补充:

(最多只允许输入30个字)