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 题意: 给定一些内存中的节点的地址,值 ...
随机推荐
- Eclipse和MyEclipse的区别
翻译:日食,月食. eclipse是免费的,myeclipse是收费的. myeclipse是eclipse的插件.
- Entity Framework 6 Code First 系列:无需修改实体和配置-在MySql中使用和SqlServer一致的并发控制
无需修改实体和配置,在MySql中使用和SqlServer一致的并发控制.修改RowVersion类型不可取,修改为Timestamp更不可行.Sql Server的RowVersion生成一串唯一的 ...
- APP耗电量专项测试整理
Android: (使用batterystats) 方法: 手机自带的电量监控.GT 命令(5.0以上系统才可以): 1.下载historian.py脚本,下载地址:https://github.co ...
- angular-messages.js信息验证的使用
ngMessages(1.3+) 众所周知,表单和验证是Angular中复杂的组件之一.上面的例子不是特别好,不简洁.在Angular 1.3发布前,表单验证必须以这种方式编写.然而在发布的Angul ...
- volatile关键字与内存可见性
前言 首先,我们使用多线程的目的在于提高程序的效率,但是如果使用不当,不仅不能提高效率,反而会使程序的性能更低,因为多线程涉及到线程之间的调度.CPU上下文的切换以及包括线程的创建.销毁和同步等等,开 ...
- 003——VUE操作元素属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Prism5.0新内容 What's New in Prism Library 5.0 for WPF(英汉对照版)
Prism 5.0 includes guidance in several new areas, resulting in new code in the Prism Library for WPF ...
- request对象和response对象,什么时候用,具体用哪一个,没有感觉
request对象和response对象,什么时候用,具体用哪一个,没有感觉
- Redis补充
Redis补充 (1)redis基本概念 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set ...
- ViewPager实现图片轮翻效果
很多App都有这种效果,特别一些电商类的App,顶部每隔几秒钟会向右翻页显示下张图片,用来作推广或者内容展示用的.今天来简单地模仿一下,还自带一个自动跳动的小功能(底部有几个小点,图片移动的时候,点的 ...