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 题意: 给定一些内存中的节点的地址,值 ...
随机推荐
- java 类的加载、连接和初始化
JVM和类 调用Java命令运行Java程序时,该命令将会启动一条Java虚拟机进程,不管该Java程序启动了多少条线程,创建了多少个变量,它们都处于该Java虚拟机进程里,共享该JVM进程的内存区. ...
- kolla all-in-one 安装
http://docs.openstack.org/developer/kolla/ 使用了Docker containers and Ansible playbooks 目前在Fedora/Ubun ...
- vector push_back数量大的时候变慢
才用15000个数据 push_back耗时就好几秒, 解决方法是 先resize 15000, 然后再 for (int i = 0; i < 15000; i++) { Data data ...
- 2015 Syrian Private Universities Collegiate Programming Contest
A. Window B. Paper Game Des:给你一个矩形集合,一开始只有一个W*H的矩形.每次可以选一个矩形,切成两个并加入集合,长和宽必须是正整数.不能操作者输,求先手赢还是输.(1 ≤ ...
- ggplot 画 条形图
今天开会谈了半天自己的研究结果,同事皱着眉头,第一好像她没大听懂,第二感觉眼前一亮,但不知怎么落地.落地这个事情,交给时间吧,我想练熟我的分析. 今天搞了个简单的,条形图. 就是EXCEL里面经常玩的 ...
- 分享知识-快乐自己:Ajax 跨域请求处理
<%-- Created by IntelliJ IDEA. User: asus Date: 2019/1/24 Time: 15:57 To change this template use ...
- 三十九 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本概念
elasticsearch的基本概念 1.集群:一个或者多个节点组织在一起 2.节点:一个节点是集群中的一个服务器,由一个名字来标识,默认是一个随机的漫微角色的名字 3.分片:将索引(相当于数据库)划 ...
- cvFindContours函数
cvFindContours函数: int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour, in ...
- Job for docker.service failed because the control process exited with error code. See "systemctl status do cker.service" and "journalctl -xe" for details.
问题出现 :入手操作Docker时,安装启动后报了这个错 Job for docker.service failed because the control process exited with e ...
- 【git】常用命令大全
Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 后边接仓库文件地址 查看远程仓库:$ gi ...