立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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 (<10^5) 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 [−10^5,10^5], 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


题意:给定一些节点,要求将链表按值升序排序并输出。

链表类型题总结(适用所有链表题):
1. 一定要将链表构造出来再进行操作,因为样例中有很多无效节点。
2. 考虑头结点为-1的情况
3. 输出单一头结点时注意头结点的也要使用%05d


#include<bits/stdc++.h>
using namespace std;
struct lnode{int value = 0, next = -1;
}node[100010];bool cmp(int x1, int x2) {return node[x1].value < node[x2].value;
}int main() {int n, fir; scanf("%d%d", &n, &fir);//头结点为-1的特殊情况 if(fir == -1) { printf("0 -1\n"); return 0; }//构建链表。这一步的目的是去除无效节点。 for(int i = 0; i < n; i++) {int x; cin >> x;cin >> node[x].value >> node[x].next;}//遍历链表,将链表中的节点压入vector。 //注意这里只存储地址即可。因为可以通过地址找到value vector<int>v;while(fir != -1) {v.push_back(fir);      //只存储地址即可。fir = node[fir].next;}sort(v.begin(), v.end(), cmp);int len = v.size();//注意这里头结点的输出也要用%05d,别忘了 printf("%d %05d\n", len, v[0]);for(int i = 0; i < len-1; i++) printf("%05d %d %05d\n", v[i], node[v[i]].value, v[i+1]);printf("%05d %d -1\n", v[len-1], node[v[len-1]].value);return 0;
}

耗时:


求赞哦~ (✪ω✪)

【附段错误原因,最后两个测试点】1052 Linked List Sorting (25 分)【链表类题目总结】相关推荐

  1. 段错误原因分析和查找

    转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 最近在Linux环境下做C语言项目,由于是在一个原有项目基础之 ...

  2. LINUX I2C驱动偶尔出现段错误原因之一

    LINUX I2C驱动偶尔出现段错误原因之一 第一篇博客,来个好的开端. 在自己写 触摸屏 I2C 驱动程序(Linux 3.14)的时候,加载的时候会偶尔出现段错误, 我们都知道在加载驱动的时候会调 ...

  3. android ndk 段错误,android crash之段错误原因及分析方法

    在解决app/frameworks客户问题的过程中经常碰到段错误的问题,在Aplog中搜索fatal关键字会碰到类似F/libc    ( 6721): Fatal signal 11 (SIGSEG ...

  4. python段错误原因_python – 捕获崩溃的子进程的“分段错误”...

    shell可能会生成"Segmentation fault"消息.要找出该过程是否被SIGSEGV杀死,请检查proc.returncode == -signal.SIGSEGV. ...

  5. 【后两个测试点】地下迷宫探索 (30 分)

    立志用最少的代码做最高效的表达 地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活 ...

  6. 【测试点分析】1010 Radix (25 分)_37行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a pair of positive integers, for example, 6 and 110, can th ...

  7. C/C++中的段错误(Segmentation fault)[转]

    Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的. 来自:http://oss.lzu.edu.cn/blog/article.php ...

  8. Linux段错误-转

    Linux段错误 目录 1.什么是段错误? 2.为什么段错误这么"麻烦"? 3.编程中通常碰到段错误的地方有哪些? 4.如何发现程序中的段错误并处理掉? 正文 1.什么是段错误? ...

  9. C/C++段错误问题排查和解决方法

     Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的. 来自:http://oss.lzu.edu.cn/blog/article. ...

最新文章

  1. 第3关:递归实现二叉树左右子树交换
  2. 百度一 29 岁程序员因使用CURL命令“篡改数据”被判有期徒刑一年九个月,并没收所有违法所得
  3. c语言实现将两个文件复制到一个文件里_Python中复制文件的9种方法
  4. 十年AI学者影响力盘点:何恺明排名第一,华人学者呈正向流入
  5. 【Verilog HDL 训练】第 04 天(竞争、冒险、译码等)
  6. java制表符_Java地位无可动摇的12个原因
  7. html 编辑xml,编辑XML\HTML时取消浏览“amp”
  8. windows 下 配置 github
  9. 【luogu P5022 旅行】 题解
  10. MySQl中文1001无标题_Mysql中字段类型不一致导致索引无效的处理办法
  11. 计算机受限制用户,由于该计算机受到限制,本次操作已被取消的解决办法
  12. Linux 字符集问题
  13. 点云配准1:配准基础及icp算法
  14. cf显示网络连接服务器失败怎么办,cf连接服务器失败怎么办
  15. 我的世界 服务器文件ess,我的世界指令大全 ess指令用法介绍
  16. MYSQL数据库版本更新
  17. c语言网络编程断点续传,网络编程(三) 下载任务,支持断点续传(示例代码)...
  18. Java程序员秋招面经大合集
  19. 计算机科学与技术专业为什么要学物理,「物理」一定要好的14个大学专业
  20. Python办公自动化,合并excel+pdf转word等

热门文章

  1. IO多路转接之epoll
  2. 2022 WebRTC发展趋势分析
  3. AVA:Netflix的剧照个性化甄选平台
  4. Revvel如何将视频转码速度提升几十倍?
  5. 腾讯携手2020全球C++及系统软件技术大会
  6. 腾讯运维技术专家集结,揭秘高效智能运维 | 沙龙报名中
  7. 大牛书单 | 人工智能方向好书分享(第二期)
  8. 使用 pyenv 管理 Python 版本
  9. WebRTC各种资料集合(WebRtc入门必看)
  10. error: uuid/uuid.h: No such file or directory