1052 Linked List Sorting
题意:链表排序
思路:题目本身并不难,但是这题仔细读题很重要。原题中有一句话,"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",它没有明确表示这个N就是链表的结点个数,而是内存中含有的结点个数(大家可以相比较另外几道链表的题目,看看题目描述的细微差别)。因此,在读入数据后,要从头结点开始遍历一遍链表,把链表上的结点提取出来,因为,输入的链表有可能是不属于这个链表的干扰结点。有几个坑需要注意——
1.输入的结点并不全是链表的结点,因此在正式排序前应先提取出有效结点;
2.有可能在第1步完成后,有效结点为0,故输出 0 -1。
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
;
struct Node{
int data;
int curr,next;
}LinkList[N],node[N];
bool cmp(Node a,Node b){
return a.data<b.data;
}
int main()
{
int n,head;
scanf("%d%d",&n,&head);
int curr,data,next;
;i<n;i++){
scanf("%d%d%d",&curr,&data,&next);
LinkList[curr].data=data;
LinkList[curr].curr=curr;
LinkList[curr].next=next;
} //提取有效结点
;
){
node[cnt++]=LinkList[head];
head=LinkList[head].next;
}
sort(node,node+cnt,cmp);
){//空链表的情况
printf("0 -1\n");
;
}
printf(].curr);
;i<cnt;i++){//注意遍历个数cnt,不是n
printf("%05d %d ",node[i].curr,node[i].data);
) printf(].curr);
else printf("-1\n");
}
;
}
常见的错误写法:
#include <cstdio>
#include <algorithm>
using namespace std;
;
struct Node{
int data;
int curr,next;
}LinkList[N];
bool cmp(Node a,Node b){
return a.data<b.data;
}
int main()
{
//freopen("pat.txt","r",stdin);
int n,head;
scanf("%d%d",&n,&head);
int curr,key,next;
;i<n;i++){
scanf("%d%d%d",&curr,&key,&next);
LinkList[i].data=key;
LinkList[i].curr=curr;
LinkList[i].next=next;
}
sort(LinkList,LinkList+n,cmp);
printf(].curr);
;i<n;i++){
printf("%05d %d ",LinkList[i].curr,LinkList[i].data);
) printf(].curr);
else printf("-1\n");
}
;
}
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 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 [一般]
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 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
- 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> ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
随机推荐
- 技术分享:HBase架构
Zookeeper,作为分布式的协调.RegionServer也会把自己的信息写到ZooKeeper中. HDFS是Hbase运行的底层文件系统 RegionServer,理解为数据节点,存储数据的. ...
- C# 捕获数据库自定义异常
在 SQL Server 的存储过程中根据业务逻辑的要求,有时需要抛出自定义异常,由C#程序俘获之并进行相应的处理.SQL Server 抛出自定义异常和简单,像这样就可以了:RAISERROR('R ...
- 智课雅思词汇---二十四、名词性后缀ary(也是形容词后缀)
智课雅思词汇---二十四.名词性后缀ary(也是形容词后缀) 一.总结 一句话总结:很多词缀即是名词词缀也是形容词词缀,很多词即是名词也是形容词 1.名词性后缀-tude? 词根词缀:-tude [来 ...
- Java反射深入浅出
在JVM中对一个类实例的创建,有两种方式,一种是编译时,一种是运行时.两种方式在开发过程中都是十分重要的.在Java中无时无刻无处不在的Java对象,实例化的过程也就变得尤为引人瞩目.我们经常用new ...
- 通用 mysql配置
windows:my.ini [mysqld] # Remove leading # and set to the amount of RAM for the most important data ...
- linux 里rpm包到底是干什么用的
Linux RPM全称是“RedHat Package Manager”,最早是Red Hat公司开发的,后来在CentOS.Fedora.SUSE都用它.而rpm包则是软件编译完成后按照RPM机制打 ...
- vue-cli favicon 怎么引入
把你的 favicon 图标放到 static 目录下,然后修改 link 标签指向 static 目录下的 favicon 图标文件即可
- python 超时异常处理
异常处理具体见:[循序渐进学Python]9.异常处理 环境平台:Python2.7.9 + Win8.1 本篇记录一下自己写爬虫的遇到的问题,程序中批量获取图片地址,然后批量保存.由于没有设置网址打 ...
- DOS命令说明
(转自:http://blog.csdn.net/mydriverc/article/details/2047152) 1 errorlevel dir cd md rd del ren cls ty ...
- Java基础摘要(一)
三大特性 封装 所谓封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏.封装是面向对象的特征之一,是对象和类概念的主要特性.简单的说,一 ...