PAT 甲级 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 (<) 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 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
题意:
给出一个链表,将链表排序,然后把链表上的结点按照data值的从小到大顺序输出。
题解:
数组模拟链表
先后使用两种不同的存储方式。
注意:
1.不一定所有的节点都在链表里(一开始没仔细读题,只得了21分)
2.链表中没有元素时候第一行不输出,第二行输出 0 -1。
AC代码:
#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int n;
struct node{
int k;
int next;
}a[];
struct node2{
int k;
int add;
}b[];
int x;
bool cmp(node2 x,node2 y){
return x.k<y.k;
}
int main(){
int root;
cin>>n>>root;
a[root].next=-;
for(int i=;i<=n;i++){
cin>>x;
cin>>a[x].k>>a[x].next;//这样存储
}
int num=;
if(a[root].next==-){//没有一个在链表里
printf("%d %d",,-);
return ;
}
while(a[root].next!=-){//排序的节点必须都在链表里
b[++num].k=a[root].k;//换一种存储节点的方式,按顺序存储
b[num].add=root;
root=a[root].next;
}
b[++num].k=a[root].k;
b[num].add=root;
sort(b+,b++num,cmp);
printf("%d %05d\n",num,b[].add);
for(int i=;i<=num;i++){
printf("%05d %d ",b[i].add,b[i].k);
if(i!=num) printf("%05d\n",b[i+].add);
else cout<<"-1";
}
return ;
}
柳神的题解写的更好,学习一下:
分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表,将在链表中的结点的flag标记为true,并且统计cnt(有效结点的个数)。(因为有的结点根本不在链表中)
然后将链表进行排序,如果flag == false就把他们移动到后面(即:reuturn a.flag > b.flag),最后只输出前cnt个链表的信息~
#include <algorithm>
using namespace std;
struct NODE {
int address, key, next;
bool flag;
}node[];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
}
int main() {
int n, cnt = , s, a, b, c;
scanf("%d%d", &n, &s);
for(int i = ; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
node[a] = {a, b, c, false};
}
for(int i = s; i != -; i = node[i].next) {
node[i].flag = true;
cnt++;
}
if(cnt == ) {
printf("0 -1");
} else {
sort(node, node + , cmp1);
printf("%d %05d\n", cnt, node[].address);
for(int i = ; i < cnt; i++) {
printf("%05d %d ", node[i].address, node[i].key);
if(i != cnt - )
printf("%05d\n", node[i + ].address);
else
printf("-1\n");
}
}
return ;
}
PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)的更多相关文章
- 【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 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)
1017 Queueing at Bank (25 分) Suppose a bank has K windows open for service. There is a yellow line ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- 1052 Linked List Sorting (25分)
题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- 【PAT甲级】1109 Group Photo (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
随机推荐
- 大数据之路week06--day03(jdk8新特性 Lambda表达式)
为什么使用Lambda表达式?(做为初学者接触这个新的语法,会很懵逼,说道理,我在接触到这一块的时候,语法规则我看到了也很懵逼,因为这个和逻辑的关系不是很大,但就是作为一种新的语法出现,一时间很难接受 ...
- Git报错:Permission denied (publickey)
Git在克隆的时候报错.Permission denied (publickey). 报错 Permission denied (publickey) 具体如下: 原因:没有将自己的电脑的SSH ke ...
- Selenium常用API的使用java语言之5-selenium元素定位
1.selenium定位方法 Selenium提供了8种定位方式. id name class name tag name link text partial link text xpath css ...
- vs编译自定义编译任务记录,msbuild
https://www.cnblogs.com/whitewolf/archive/2011/07/27/2119005.html http://www.cnblogs.com/hjf1223/arc ...
- 03_Tutorial 3: Class-based Views 基于类的视图(CBV)
1.CBV 0.文档 https://q1mi.github.io/Django-REST-framework-documentation/tutorial/3-class-based-views_z ...
- vue:概要
一.环境 #安装nodejs-官网安装包配置环境变量 node -v #安装webpack npm install webpack -g #安装vue-cli npm install vue-cli ...
- Greenplum 调优--数据倾斜排查(二)
上次有个朋友咨询我一个GP数据倾斜的问题,他说查看gp_toolkit.gp_skew_coefficients表时花费了20-30分钟左右才出来结果,后来指导他分析原因并给出其他方案来查看数据倾斜. ...
- 005_Python3 运算符
什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算 ...
- C# 使用递归获取所有下属、所有子部门……
本例中获取的是所有的晚辈!首先定义家庭成员类: public class FamilyMember { /// <summary> /// 身份 /// </summary> ...
- zabbix数据的时序-
gj的proxy服务器经过重启之后时序有变化. zabbix数据库中数据的存储是以哪方为准server端还是agent端, 触发事件跟恢复时间反了,本应该恢复的事件在数据库中查询event,得到的事件 ...