PAT 解题报告 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 (< 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
题目描述:
给一个链表按照node里面存着的key排序.
算法分析:
算法很简单就是排序算法, 调用现成的sort之类的库就行了, 注意两点”
(1) 给定的node不一定都是在同一个链表上 (这就是为什么我们需要head node 的address)
(2) head node address 可能为-1,小心segment fault.
(3) 如果链表是空,应当输出”0 -1″.
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
#define MX 100001
struct Node {
int key, val, next;
};
//不用vector,而用数组的好处是,数组可作为hash
Node m[MX], linked[MX];
bool cmp(Node p, Node q) {
return p.val<q.val;
} int main()
{
int N, head;
scanf("%d%d", &N, &head);
for (int i=; i<N; i++) {
int tmp;
scanf("%d", &tmp);
scanf("%d%d", &m[tmp].val, &m[tmp].next);
m[tmp].key = tmp;
}
int cnt=;
while (head != -) {
linked[cnt++] = m[head];
head = m[head].next;
} sort(linked, linked+cnt, cmp);
if (cnt == ) printf("0 -1");
else {
printf("%d %05d\n", cnt, linked[].key);
for (int i=; i<cnt;i++) {
if (i!=cnt-) {
printf("%05d %d %05d\n", linked[i].key, linked[i].val, linked[i+].key);
}
else {
printf("%05d %d -1", linked[i].key, linked[i].val);
}
}
} return ;
}
PAT 解题报告 1052. Linked List Sorting (25)的更多相关文章
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- 【PAT】1052 Linked List Sorting (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
随机推荐
- coursera python 学习总结
为啥要写这篇总结?早上突然想到了四个字:知行合一.实践,总结,再实践,再总结经验,积累经验,为己所用.闲话少叙,来干货: 1.目标要单一,如果想要完成课程,还要健身,还要玩玩游戏.看看电影,还学别的课 ...
- buffer overflow vulnerabilitie
Computer Systems A Programmer's Perspective Second Edition Avoiding security holes.For many years,bu ...
- pdftoswf + flexpaper 图片转pdf浏览体验的实现
需要的工具: pdftoswf:http://www.swftools.org/download.html flexpaper包,最好找个破解版的swf文件替换一下里面的swf文件.http://fi ...
- 高级 Synth
http://www.ibm.com/developerworks/cn/java/j-synth/
- javax.management.NotCompliantMBeanException
public interface QueueMBean { } 假如接口名叫 XMBean ,那么实现名就必须一定是X,而且是大小写敏感的. public class Queue implements ...
- [LeetCode] Decode Ways(DP)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- NuGet的几个小技巧
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- 简单计算器--hdu1237(栈的运用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 这是单纯的本题答案: #include<stdio.h> #define N 11 ...
- xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
用Xcode7向App Store提交二进制文件是,提示ERROR ITMS-90096:"You binary is not optimized for iPhone 5...." ...