博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1052 Linked List Sorting
阅读量:4540 次
发布时间:2019-06-08

本文共 2513 字,大约阅读时间需要 8 分钟。

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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345

Sample Output:

5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1

注意点

(1)题目给出的结点中可能有不在链表中的无效结点

(2)最后一个测试点测试的是首地址为 - 1,即为空链表的情况,此时只输出0 - 1

(3)输出时结点地址除 - 1外要有5位数字,不够则在高位补0 。所以地址 - 1要进行特判输出

1 #include 
2 #include
3 using namespace std; 4 int main() 5 { 6 int N, head, maxV = -1000000; 7 cin >> N >> head; 8 map
value; 9 map
>data;10 for (int i = 0; i < N; ++i)11 {12 int addr, val, next;13 cin >> addr >> val >> next;14 data.insert(make_pair(addr, make_pair(val, next)));15 }16 if (data.find(head) == data.end())17 {18 cout << 0 << " " << -1 << endl;19 return 0;//该链表为空20 }21 while (head != -1)//遍历一下数据,找到是链表中的数据22 {23 value.insert(make_pair(data[head].first, head));24 head = data[head].second;25 }26 printf("%d %05d\n", value.size(), value.begin()->second);27 for (auto ptr = value.begin(); ptr != value.end(); ++ptr)28 {29 if (ptr == value.begin())30 printf("%05d %d ", ptr->second, ptr->first);31 else32 printf("%05d\n%05d %d ", ptr->second, ptr->second, ptr->first);33 }34 printf("%d\n", -1);35 return 0;36 }

 

 

转载于:https://www.cnblogs.com/zzw1024/p/11281097.html

你可能感兴趣的文章
javascript 数组以及对象的深拷贝(复制数组或复制对象)的方法
查看>>
mac office2016
查看>>
Activity
查看>>
计算机日语
查看>>
04-关键字、标识符、注释
查看>>
01-HTML基础与进阶-day4-录像247
查看>>
mysql表操作
查看>>
Flask Web开发从入门到放弃(一)
查看>>
数组的完全随机排列
查看>>
cuda和显卡驱动版本
查看>>
LeetCode OJ
查看>>
你的焦虑迷茫真的不是因为太懒太闲?
查看>>
Autolayout + VFL(Visual Format Language)
查看>>
通过虚拟驱动vivi分析摄像头驱动
查看>>
【JZOJ100208】【20190705】传说之下
查看>>
面试小记
查看>>
线性代数
查看>>
网页设计
查看>>
批量删除空行
查看>>
Java输入
查看>>