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 题意: 给定一些内存中的节点的地址,值 ... 
随机推荐
- 一百、SAP中ALV事件之十三,给ALV的自定义按钮添加事件
			一.我们查看定义的按钮,有一个名字是ZADD的自定义按钮 二.代码如下,用于判断点击了哪个按钮 三.点击测试按钮之后,会弹出一个弹窗 完美 
- JAVA - SpringBoot项目跨域访问
			JAVA - SpringBoot添加支持CORS跨域访问 CORS(Cross-Origin Resource Sharing)“跨域资源共享”,是一个W3C标准,它允许浏览器向跨域服务器发送Aja ... 
- Java的包装类
			一.概述 因为基本数据类型的变量身上没有任何的方法和属性,所以针对基本数据类型提供了对应的类形式--包装类. 利用这个类产生对象,调用对象身上的方法来操作这个数据. 二.分类 包装类分为以下几种: 基 ... 
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring JDBCTemplate简介
			Spring 框架针对数据库开发中的应用提供了 JDBCTemplate 类,该类是 Spring 对 JDBC 支持的核心,它提供了所有对数据库操作功能的支持. Spring 框架提供的JDBC支持 ... 
- 吴裕雄--天生自然C++语言学习笔记:C++ 基本的输入输出
			C++ 的 I/O 发生在流中,流是字节序列.如果字节流是从设备(如键盘.磁盘驱动器.网络连接等)流向内存,这叫做输入操作.如果字节流是从内存流向设备(如显示屏.打印机.磁盘驱动器.网络连接等),这叫 ... 
- linux命令之strace简单使用
			strace是什么 strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. 使用方式 strace 使用帮 ... 
- ROS2学习日志:TurtleSim测试日志(基于ROS2 Eloquent Elusor)
			TurtleSim测试日志(基于ROS2 Eloquent Elusor) 1.ros2 run 1.1 ros2 run turtlesim turtlesim_node --ros-args -- ... 
- sqlserver 联接查询的一些注意点
			1.内连接的安全性 (1) inner join 是ANSI SQL-92 语法.等值联接是ANSI SQL-89 的语法 ,两者已相同方式解释.在性能上没有差别 (2)但是强烈建议使用ANSI SQ ... 
- comparable and comparator 比较
			转:http://www.yingjiesheng.com/job-002-393-132.html 一.前言 在Java集合框架里面,各种集合的操作很大程度上都离不开Comparable和Com ... 
- Codeforces_448C 分治
			昨晚CF碰到的题目,昨晚CF跪了啊啊啊 题意比较简单,给定一排挨在一起的板子,宽度都为1,高度不一,一个刷子宽度也是1,可以横着刷,也可以竖着刷,但是任何时刻刷子都要在板子上,也就是说,如果横向的时候 ... 
