PAT Advanced 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
题目分析
已知N个节点,将节点按照值排序(升)
解题思路
- 定义结构体数组,保存所有结点信息,节点中定义标记属性flag(初始化为false)
- 遍历所有结点信息,将出现的节点flag标记为true,并定义计数器统计链表中结点数count
- 遍历链表结点,修改next值为下一个结点
易错点
- 题目中不容易分析出结点中有无效结点
- 地址格式必须为"%05d"
- 若结点数为0,打印"0 -1"
知识点
- 将无效结点排序到最后的办法
!a.flag || !b.flag ? a.flag > b.flag
//降序true=1在前,false=0在后
Code
Code 01
#include <iostream>
#include <algorithm>
using namespace std;
struct NODE {
int address, key, next;
bool flag;
} node[100000];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
}
int main() {
int n, cnt = 0, s, a, b, c;
scanf("%d%d", &n, &s);
for(int i = 0; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
node[a] = {a, b, c, false};
}
for(int i = s; i != -1; i = node[i].next) {
node[i].flag = true;
cnt++;
}
if(cnt == 0) {
printf("0 -1");
} else {
sort(node, node + 100000, cmp1);
printf("%d %05d\n", cnt, node[0].address);
for(int i = 0; i < cnt; i++) {
printf("%05d %d ", node[i].address, node[i].key);
if(i != cnt - 1)
printf("%05d\n", node[i + 1].address);
else
printf("-1\n");
}
}
return 0;
}
PAT Advanced 1052 Linked List Sorting (25) [链表]的更多相关文章
- 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】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) A linked list consists of a series of structures, which are not neces ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
随机推荐
- Linux 下 zip 文件解压乱码如何解决
作者:Latm Ake链接:https://www.zhihu.com/question/20523036/answer/35225920来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...
- kafka创建topic,生产和消费指定topic消息
启动zookeeper和Kafka之后,进入kafka目录(安装/启动kafka参考前面一章:https://www.cnblogs.com/cici20166/p/9425613.html) 1.创 ...
- phpStudy配置站点解决各种不能访问问题(本地可www.xx.com访问)
1.配置站点:打开phpStudy->其他选项菜单->站点域名管理 2.配置站点:打开phpStudy->其他选项菜单->打开hosts(www访问重点) 3.在apache的 ...
- webpack知识点散记
1.今天学习webpack ,刚开头就碰到了钉子,因为现在都是4+的版本,用以前的老命令不好使,如下例子及解决办法 不好用: webpack3的 打包文件 webpack a.js b.j ...
- (2) JVM内存管理:垃圾回收
回顾上期 1)JVM中引用存在哪里? 答:虚拟机栈,该内存空间线程独有 2)该引用的对象存在哪里? 答:堆,所有通过new方法分配的对象都存在堆中 3)String s1="abc" ...
- Thread.currentThread()和this的区别
1. Thread.currentThread()可以获取当前线程的引用,一般都是在没有线程对象又需要获得线程信息时通过Thread.currentThread()获取当前代码段所在线程的引用. 2. ...
- Java TCP发送与接收
IP地址?端口号?主机名? 什么是Socket? 什么是UDP? 什么是TCP? UDP和TCP区别? 以上问题请自行百度,有标准解释,此处不再赘述,直接上干货! 实例: 发送端: public cl ...
- VS.NET中的常用控件和类型的命名规范
表1 命名规范 VS名称 简写 VS名称 简写 数据类型 Array arr Boolean bln Byte byt Char Chr Date Time dtm Decimal dtm Doub ...
- PyMySQL学习笔记
一些常用函数及解释 db = pymysql.connect('host','user','password','database') # 连接数据库 cursor = db.cursor() # 创 ...
- [DDCTF 2019]homebrew event loop
0x00 知识点 逻辑漏洞: 异步处理导致可以先调用增加钻石,再调用计算价钱的.也就是先货后款. eval函数存在注入,可以通过#注释,我们可以传入路由action:eval#;arg1#arg2#a ...