PAT 1052 Linked List Sorting [一般]
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
题目大意:给出一个链表,按照其中存储的key值大小重新排序。
最终AC:
#include <iostream>
#include <algorithm>
#include<cstdio>
#include <vector>
using namespace std; struct Node{
int now,num,next;
}node[];
vector<Node> vt;
bool cmp(Node& a,Node& b){
return a.num<b.num;
}
int main()
{
int n,f;
scanf("%d%d",&n,&f);
int t,num,next;
for(int i=;i<n;i++){
cin>>t>>num>>next;
node[t].num=num;
node[t].next=next;
node[t].now=t;
}
for(int index=f;index!=-;index=node[index].next){
vt.push_back(node[index]);
} //有一个疑问就是如果输入数据为0 0 ,怎么输出?
if(vt.size()==){
printf("0 -1");
}else{
sort(vt.begin(),vt.end(),cmp);
printf("%d %05d\n",vt.size(),vt[].now);
for(int i=;i<vt.size();i++){
if(i!=vt.size()-){
printf("%05d %d %05d\n",vt[i].now,vt[i].num,vt[i+].now);
}else{
printf("%05d %d -1",vt[i].now,vt[i].num);
}
}
}
return ;
}
//自己遇到了很多坑。。。
1.对于输入是0 0不知道如何输出,看了别人的发现是输出0和-1,记住吧,以后对于链表长度为0的,就是输出数量0,然后直接就是结束-1.
2.第一次提交时有段错误,感觉到是数组定义长度的问题,一开始定义长度为100000,后来又加了10可以了,最后一个测试点,那么以后在定义时就比平常多加10就可以啦
3.之后提交发现第4个测试点过不去,这是为什么呢?去牛客网上发现是如下错误:
测试用例: - 对应输出应该为: - 你的输出为:
最终在输出起点坐标时加上了%05d,就可以了。。。。
PAT 1052 Linked List Sorting [一般]的更多相关文章
- 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
这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发.估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过 #include <iost ...
- 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)(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 ne ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- 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 (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- 在windows中将Tomcat作为服务启动
http://www.cnblogs.com/chuyuhuashi/archive/2012/04/28/2475315.html ————————————————————————————————— ...
- javah 错误: 找不到 'com.example.tony.gpiojni.JNITest' 的类文件
在 android studio的Terminal中运行javah转换.class文件为.h文件失败, 提示: 错误: 找不到 'com.example.tony.gpiojni.JNITest' 的 ...
- Android Studio 编写 JNI
之前一直都不知怎么编写JNI,今天刚好学习一下,感谢梦真的指教,以及提供的文档. 参考链接 http://blog.csdn.net/u011168565/article/details/518781 ...
- iOS越狱系统使用root权限运行命令
//命令原型:sh -c "echo 密码 | su -c 'ls --help' " //转载请注明:http://www.cnblogs.com/bandy/p/7069503 ...
- 如何用ChemDraw中的ChemFinder查询反应过程
ChemFinder是ChemDraw化学绘图软件的重要插件之一,ChemFinder是一个贮存众多化学信息的数据库管理系统,不仅可以用于查询基本化学结构,用户还可以用ChemFinder查询需要的反 ...
- Rename a local and remote branch in git
If you have named a branch incorrectly AND pushed this to the remote repository follow these steps b ...
- docker实用命名
删除tag/镜像: #删除tag docker rmi index-dev.qiniu.io/cs-kirk/nginx:latest docker rmi index-dev.qiniu.io/cs ...
- Struts2_day01--访问action的方法
访问action的方法(重点) 1 有三种方式实现 第一种 使用action标签的method属性,在这个属性里面写执行的action的方法名称 第二种 使用通配符方式实现 第三种 动态访问实现(不用 ...
- Hibernate_day04--QBC查询
QBC查询 1 使用hql查询需要写hql语句实现,但是使用qbc时候,不需要写语句了,使用方法实现 2 使用qbc时候,操作实体类和属性 3 使用qbc,使用Criteria对象实现 查询所有 1 ...
- ArcGIS GP应用-GP模型服务发布
1.双击模型名称打开运行窗体 2.在图上交互和窗体中输入数据后,点击确定运行模型,查看运行结果 3.在当前模型“缓冲区分析”的目录树上,右击含有图形(点.线.面)的节点,点击添加至显示,显示图片 4. ...