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> ...
随机推荐
- MySQL5.0、5.1、5.5、5.6功能进化
目前线上使用的版本情况:新上线端口统一使用5.5,不说别的,一个快速恢复重启就值回票价. 但因为历史原因还有大量5.1的版本,甚至,I’am sorry,还有少数5.0的版本. 至于5.0以前的版本, ...
- SQL Server 2012附加数据库报错
操作系统: win8 数据库:SQL 2012 遇到问题: 以管理员身份登录SQL 2012,附件数据库提示如下错误: 解决办法: 以windows账号登录,附加,成功!
- 常用的easyui使用方法之二
-------datagrid 1.获取某行的行号(row)tdg.datagrid('getRowIndex',rows)2.通过行号移除该行tdg.datagrid('deleteRow',ind ...
- linux文件目录连接
linux系统下提供ln指令来进行文件链接.文件链接主要分为硬链接和软链接. 硬链接:由于linux下的文件是通过索引节点(Inode)来识别文件,硬链接可以认为是一个指针,指向文件索引节点的指针,系 ...
- 使用ANT编译项目报错 com.sun.image.codec.jpeg does not exist 解决方法
项目开发中在对图片进行裁切处理的时候,有时候是会使用到 com.sun 包下的类时. 假设项目使用ant编译,会出现错误 com.sun.image.codec.jpeg does not exist ...
- es6 javascript的class的静态方法、属性和实例属性
http://blog.csdn.net/qq_30100043/article/details/53542966
- eq与gt的妙用
应用到jq中: 一.jquery :gt选择器: 定义: :gt 选择器选取 index 值高于指定数的元素. 语法:$(":gt(index)") ex:$("l ...
- log4j日志服务器配置
可参考的文章: http://www.jb51.net/article/89597.htm http://www.jb51.net/article/41001.htm http://liuzhijun ...
- 170310、Jenkins部署Maven多环境项目(dev、beta、prod)的参数设置
使用Jenkins配置Git+Maven的自动化构建: http://blog.csdn.net/xlgen157387/article/details/50353317 在一个多开发和生产环境的项目 ...
- Spring中的事物管理----HelloWorld
在学习Spring的事物管理之前明白先明白几个概念1什么是事物:事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用例子说明:例如银行转账,A账户转账(转2 ...