PAT A1052. Linked List Sorting (25)
来源:互联网 发布:张馨予的淘宝店铺名字 编辑:程序博客网 时间:2024/06/06 03:43
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 (< 105) 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 [-105, 105], 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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1
#include <cstdio>#include <stdlib.h>#include <algorithm>#include <vector>#include <cstring>#include <math.h>#include <string>#include <iostream>using namespace std;#define Max 100010struct LNODE{int add; int data; int next; int get;}node[Max];bool cmp(LNODE a,LNODE b){if(a.get==0||b.get==0) return a.get>b.get;else return a.data<b.data;}int main(){for(int i=0;i<Max;i++){node[i].get=0;}int n,m,ad,flag=0;scanf("%d %d",&n,&m);for(int i=0;i<n;i++){scanf("%d",&ad);scanf("%d %d",&node[ad].data,&node[ad].next);node[ad].add=ad;}ad=m; while (ad!=-1) { node[ad].get++; flag++; ad=node[ad].next; }if(flag==0){printf("0 -1");}else {sort(node,node+Max,cmp);ad=node[0].add;for(int i=1;i<=flag;i++){node[i-1].next=node[i].add;}printf("%d %05d\n",flag,ad);for(int i=0;i<flag;i++){if(i!=flag-1){printf("%05d %d %05d\n",node[i].add,node[i].data,node[i].next);}else {printf("%05d %d -1\n",node[i].add,node[i].data);}}}system("pause");return 0;}
- PAT A1052. Linked List Sorting (25)
- pat-a1052. Linked List Sorting (25)
- A1052. Linked List Sorting (25)
- A1052.Linked List Sorting
- PAT-Linked List Sorting (25)
- 1052. Linked List Sorting (25)-PAT
- 【PAT】1052. Linked List Sorting (25)
- PAT (Advanced) 1052. Linked List Sorting (25)
- PAT A 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- PAT 1052. Linked List Sorting (25)
- Pat 1052 Linked List Sorting (25)
- PAT(A) - 1052. Linked List Sorting (25)
- 【PAT甲级】1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25) PAT甲级
- PAT甲级1052. Linked List Sorting (25)
- PAT-A-1052. Linked List Sorting (25)
- 十、用scikit-learn的网格搜索快速找到最优模型参数
- Android ViewPager 页面生成缓冲保留
- Tensorflow 数据预读取--Queue
- HDU 3652 数位DP变形
- 【JAVA笔记】JAVA后端实现统一扫码支付:微信篇
- PAT A1052. Linked List Sorting (25)
- 深入右值引用,move语义和完美转发
- 十一、用scikit-learn做聚类分析
- PHP 错误处理
- oracle 性能调整综述
- 10倍效率”程序员/开发人员的习惯
- 十二、神经网络模型的原理
- 数据库取别名
- eclipse重要快捷键