PAT甲级1052 Linked List Sorting:[C++题解]链表排序
文章目录
- 题目分析
- 题目链接
题目分析
题意:给定数据(里面有不构成链表的数据,若是,则跳过),是链表的构成链表。然后根据数值大小重新排序,构成新的链表。
分析:用数组模拟链表,先建立链表。遍历链表,用pair来存 <结点的地址,数值>,存入vector。然后对vector排序,然后就是输出即可。
ac代码
#include<bits/stdc++.h>
using namespace std;typedef pair<int,int> PII; //地址 ,数据
const int N=1e5+10;
int n,h;
int e[N],ne[N];vector<PII> res;bool cmp(PII a, PII b){return a.second < b.second;
}int main(){cin>>n>>h;if(h!=-1){ //非空链表for(int i=0;i<n;i++){int address , data , next;cin>>address>> data >>next;e[address] =data, ne[address]=next;}for(int i=h;i!=-1;i=ne[i]){res.push_back({i,e[i]}); //下标 和 数据}sort(res.begin(),res.end(),cmp);printf("%d %05d\n",res.size(),res[0].first);for(int i=0;i<res.size();i++){printf("%05d %d ",res[i],e[res[i].first]);if(i+1 == res.size()) cout<<-1<<endl;else printf("%05d\n",res[i+1].first);}}else{ //空链表cout<<0<<" "<<-1<<endl;}}
题目链接
PAT甲级1052 Linked List Sorting
PAT甲级1052 Linked List Sorting:[C++题解]链表排序相关推荐
- PAT甲级1146 Topological Order :[C++题解]拓扑排序、结构体存边、图论
文章目录 题目分析 题目链接 题目分析 来源:acwing 拓扑排序是对有向无环图的顶点的一种排序,它使得若存在一条从顶点A到顶点B的路径A→B,则在排序中顶点B出现在顶点A的后面. 根据概念,可以想 ...
- PAT甲级1138 Postorder Traversal:[C++题解]前序遍历和中序遍历建树
文章目录 题目分析 题目链接 题目分析 做过前面几道题,发现这道题就是一道模板题,递归建树即可. 还是使用笔者熟悉的hash表来找根,进行优化. 请移步至笔者的另一篇文章:PAT甲级1020 Tree ...
- 1052. Linked List Sorting
没仔细审题,走了很多弯路.题目要求:内存中有很多散列的节点,可能构成不止一个链表.其次要注意空链表的情况. // 1052. Linked List Sorting.cpp: 主项目文件.#inclu ...
- 1052 Linked List Sorting (25 分)
Sample Input: 5 00001 11111 100 -1 00001 0 22222 33333 100000 11111 12345 -1 33333 22222 1000 12345 ...
- 1052 Linked List Sorting(排序)
1052 Linked List Sorting(排序) 思路: s t r u c t + struct+ struct+排序. 坑点: 1.答案只包含从原链表中头结点开始的结点,有点结点不在原链表 ...
- PAT甲级1133 Splitting A Linked List:[C++题解]链表
文章目录 题目分析 题目链接 题目分析 题意:链表分组的感觉.开三个vector.然后遍历链表,数据小于0的结点放入一个数组,数据位于[0,K]的放入一个数组,数据大于K的结点放入一个数组.然后输出即 ...
- 【附段错误原因,最后两个测试点】1052 Linked List Sorting (25 分)【链表类题目总结】
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A linked list consists of a series of structures, which are not n ...
- PAT甲级1051 Pop Sequence:[C++题解]模拟栈、判断序列是否是合法的出栈序列
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 题意:将1~N压栈,判断给定序列是否是合法的出栈序列. 对于序列1~N中的每个值i,先将其压入栈.然后对于它就有两种处理方法:要么压 ...
- PAT甲级1085 Perfect Sequence :[C++题解]双指针
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:求满足条件M≤m×pM \leq m\times pM≤m×p的区间[m, M]最长是多少.此处有一性质:当最大值M变大的时候,最小值 ...
最新文章
- qt on android 桌面鼠标事件,Qt on Android 不能自动创建Qt套件的问题的解决
- 有史以来的第一个脚本 找出三个数的最大数字
- eclipse如何修改dynamic web module version
- Oracle ASM 翻译系列第七弹:高级知识 How many partners?
- ---WebCam网络摄像头9 usb dirver
- Redis学习之集群(五)
- Python的小特别
- NHibernate初学者指南(10):一级和二级缓存
- 盘点最经典的外包案例
- 微软职位内部推荐-Principal Group Program Manager
- 傅里叶变换的意义 .
- 架构,改善程序复用性的设计~第二讲 什么应该提取出来,什么应该保留
- 利息高的贷款通过率会高一些吗?
- Windows下安装Elasticsearch
- windows下硬盘的逻辑结构
- 聊聊spring for kafka对consumer的封装与集成 1
- Keil MDK使用方法
- python打开360浏览器_Selenium安装与360浏览器使用
- Jmeter之BeanShell详解
- unity3d显示c4d材质_C4D入门到精通,没那么难