PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464
题意:
给定一些内存中的节点的地址,值以及下一节点所在地址。
要求对给定的头指针表示的链表进行排序。
思路:
PAT的题目小细节真的很多啊。这道题要注意有可能有的节点是不在头指针链成的链表里的。
所以要先遍历一次链表,然后再排序。
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, head;
struct node{
int key;
int nxt;
}l[];
struct num{
int add;
int key;
}tmp[]; bool cmp(num a, num b)
{
return a.key < b.key;
} int main()
{
scanf("%d%d", &n, &head);
for(int i = ; i < n; i++){
int a;
scanf("%d", &a);
scanf("%d%d", &l[a].key, &l[a].nxt);
} int now = head;
int cnt = ;
while(now != -){
tmp[cnt].add = now;
tmp[cnt].key = l[now].key;
now = l[now].nxt;
cnt++;
}
sort(tmp, tmp + cnt, cmp);
if(cnt == ){
printf("0 -1\n");
}
else{
printf("%d %05d\n", cnt, tmp[].add);
for(int i = ; i < cnt - ; i++){
printf("%05d %d %05d\n", tmp[i].add, tmp[i].key, tmp[i + ].add);
}
printf("%05d %d -1\n", tmp[cnt - ].add, tmp[cnt - ].key);
} return ;
}
PAT甲级1052 Linked List Sorting的更多相关文章
- 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甲级——A1052 Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- 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)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- 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 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
随机推荐
- oradim新建服务后,登录数据库报ORA-12560错误
> oradim -new -sid mydb 实例已创建. > sqlplus /nolog SQL*Plus: Release 11.2.0.4.0 Production on 星期二 ...
- Linux使用图形LVM(Logical Volume Manager)工具进行分区的动态扩展
- html中<a>标签的种类
在html中a 标签是一个链接标签,然而a 标签也有非常多的种类,在此做一个小结. 一.普通链接 <a href="http://www.baidu.com">百度&l ...
- 数据源从druid迁移到HikariCP
最近正好在做新项目,使用的是druid数据源,也真是巧,有朋友建议我使用HikariCP这个数据源,可以说是牛的一笔,速度快的飞起,性能极高! 要比druid不知道好多少倍,druid其实在某些情况下 ...
- 【iCore4 双核心板_FPGA】例程十二:基于UART的ARM与FPGA通信实验
实验现象: 1.先烧写ARM程序,然后烧写FPGA程序. 2.打开串口精灵,会接收到字符GINGKO. 3.通过串口精灵发送命令可以控制ARM·LED和FPGA·LED. 核心代码: int main ...
- .io域名在申请SSL证书时被坑
注:标题其实与最后内容不符,只是描述一个事实 - 遇到坑了,不代表观点 - io域名坑我. .io后缀的域名是英属印度洋领地的国别域名,由NIC.io(runby Internet Computer ...
- python安装模块
pychram安装模块,非常简单!
- Go指南练习_斐波纳契闭包
源地址 https://tour.go-zh.org/moretypes/26 一.题目描述 让我们用函数做些好玩的事情. 实现一个 fibonacci 函数,它返回一个函数(闭包),该闭包返回一个斐 ...
- python初级 2 字符串格式化
一.回顾 上次我们讲解的数据类型,有int,float,str,bool,NoneType五种 前两种和后两种都比较简单,str较为复杂 二.字符串格式化的使用场景: 有一种字符串, xxx 的内容都 ...
- Linux部署Web应用程序超链接下载中文名称文件404问题解决办法
Web应用程序目录下有帮助文档,是中文名称的Word文件 超链接内容如下: <a href="jsp/plugin/用户手册.doc">用户手册</a> 开 ...