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 ...
随机推荐
- Windows上python开发--2安装django框架
Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...
- /etc/hosts.conf
一 作用 指定如何解析主机域名.可设置网络安全. 二 参数说明 默认情况,/etc/hosts.conf 文件有如下内容—— order hosts,bind ...
- SET ? DECLARE
http://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html http://dev.mysql.com/doc/refman/5 ...
- Pentium II paging mechanism
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To understand the str ...
- CentOS + Nginx + PHP-FPM(FastCGI) 配置CodeIgniter
nginx官方现在已经针对centos提供了repository,所以现在可以直接通过yum来安装啦,很方便. nginx官方安装教程:http://nginx.org/en/download.htm ...
- 【转】下载量最高的 100 个 Laravel 扩展包推荐
说明 Laravel 另一个令人喜欢的地方,是拥有活跃的开发者社区,而活跃的开发者社区带来的,是繁华的扩展包生态. 本文对 Packagist 上打了 Laravel 标签 的扩展包进行整理,截止到现 ...
- [转]正则表达式相关:C# 抓取网页类(获取网页中所有信息)
using System; using System.Data; using System.Configuration; using System.Net; using System.IO; usin ...
- nRF51822之app_button控制uart的开启和关闭
为什么要使用app_button来控制uart的开启和关闭 还是先上datesheet中uart开启的时候需要HFCLK,需要消耗大量大电流.所以在我们需要的时候需要通过io来通知nrf51822开启 ...
- nrf51822裸机教程-GPIO
首先看看一下相关的寄存器说明 Out寄存器 输出设置寄存器 每个比特按顺序对应每个引脚,bit0对应的就是 引脚0 该寄存器用来设置 引脚作为输出的时候的 输出电平为高还是低. 与输出设置相关的 还有 ...
- eclipse dbviewer,eclipse java8
进入/home/xxx(用户名)/.local/share/applications,看是否有eclipse和深度音乐desktop配置文件,为eclipse.desktop配置图标, 那现在终端输入 ...