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> ...
随机推荐
- 关于Unity的C#基础学习(一)
一.程序包含 1.数据:运行过程中产生的 2.代码:代码指令 数据和代码都是存放到内存中的,代码指令在程序加载的时候放到内存,数据是在程序运行的时候在内存中动态地生成,随时会被回收,要定义变量来存放数 ...
- Boosting with Abstention
论文提出了一种loss: x是原始数据,y是对应的label,h(x)是一个判别函数,r(x)相当于训练了一个信心函数,r(x)越大,代表对自己做的判断的信息越大,当r(x)<0的时候,就拒绝进 ...
- 在函数体的“出口处”,对 return 语句的正确性和效率进行检查
在函数体的“出口处”,对 return 语句的正确性和效率进行检查. 如果函数有返回值,那么函数的“出口处”是 return 语句. 我们不要轻视 return 语 句.如果 return 语句写得不 ...
- linux -- Ubuntu修改静态IP地址重启后无法上网的解决
ubuntu设置静态IP地址后,上不了网 文章中也提到,如果是在/etc/resolv.conf添加DNS,由于Ubuntu 有一个 resolvconf 服务,如果重启它,那么 /etc/resol ...
- css 动画
CSS3动画相关的几个属性是:transition, transform, animation:我分别理解为过渡,变换,动画.虽意义相近,但具体角色不一.就像是SHE组合,虽然都是三个女生,都唱同一首 ...
- 浅谈AutoResetEvent的用法
using System;using System.Threading; namespace AutoResetEvent_Examples{ class MyMainClass { ...
- C++ STL迭代器与索引相互转换
0 前言 C++ STL提供了vector.list等模板容器,极大地方便了编程使用. “遍历”是对容器使用的最常用的操作. 使用迭代器来遍历是最好最高效的遍历方法. 当然,对于有些容器的遍历除了使用 ...
- THINKPHP5获取设置缓存的例子
在THINKPHP5中 缓存的配置放在了config.php文件中 代码如下 如何设置缓存? 可以使用静态方法 Cache::set('key',$value,3600);//存储缓存 Cache:: ...
- HashMap实现原理、核心概念、关键问题的总结
简单罗列一下较为重要的点: 同步的问题 碰撞处理问题 rehash的过程 put和get的处理过程 HashMap基础: HashMap的理论基础:维基百科哈希表 JDK中HashMap的描述:Has ...
- shell脚本学习总结08--比较与测试
程序中流程控制是由比较语句和测试语句处理的 算数比较 [ $var -gt 20 ] [ $var -gt 20 -a $var -lt 50 ] #使用逻辑与 [ $var -ne ...