1. 开始测试点4不通过,得分24/25,是忽略了所有节点都不在链表上的特殊情况。

2. 其实就是用静态链表,把结点根据值的大小,升序排列,所以一开始把每个结点的key赋值为超出最大值的maxn,是为了方便输出。

3. 并非给的所有结点都在链表上,用值inLink,并赋初始值为0,如果在链表上赋值为1,再在排序函数比较key之前比较inLink,这样确保输出的都是链表上的结点。

4. 在排序之后,把所有点的next赋值为下一个点的add,最后一个点的next赋值为-1。

AC代码

#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<cstring>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>using namespace std;const int maxn = 100010;struct Node{int add,key = maxn,next;//key的初始值最大,方便排序时无效结点沉到下面int inLink = 0;
}node[maxn];bool cmp(Node a,Node b){if(a.inLink!=b.inLink)return a.inLink>b.inLink; else return a.key<b.key;
}int main(){int n,begin;scanf("%d %d",&n,&begin);int add;while(n--){scanf("%d",&add);scanf("%d %d",&node[add].key,&node[add].next);node[add].add = add;}//通过inLink赋值为1,筛除那些不在链上的点int L = begin;//也许所有结点都不在链表上呢 if(L==-1){printf("0 -1\n");return 0;}int cnt = 0;while(L!=-1){node[L].inLink = 1;cnt ++;L = node[L].next;} sort(node,node+maxn,cmp);for(int i=0;i<cnt-1;i++){node[i].next = node[i+1].add;}node[cnt-1].next = -1;printf("%d %05d\n",cnt,node[0].add);for(int i=0;i<cnt;i++){printf("%05d %d",node[i].add,node[i].key);if(node[i].next!=-1)printf(" %05d\n",node[i].next);else printf(" -1\n");}return 0;
}

1052 Linked List Sorting相关推荐

  1. PAT甲级1052 Linked List Sorting:[C++题解]链表排序

    文章目录 题目分析 题目链接 题目分析 题意:给定数据(里面有不构成链表的数据,若是,则跳过),是链表的构成链表.然后根据数值大小重新排序,构成新的链表. 分析:用数组模拟链表,先建立链表.遍历链表, ...

  2. 1052. Linked List Sorting

    没仔细审题,走了很多弯路.题目要求:内存中有很多散列的节点,可能构成不止一个链表.其次要注意空链表的情况. // 1052. Linked List Sorting.cpp: 主项目文件.#inclu ...

  3. 1052 Linked List Sorting (25 分)

    Sample Input: 5 00001 11111 100 -1 00001 0 22222 33333 100000 11111 12345 -1 33333 22222 1000 12345 ...

  4. 1052 Linked List Sorting(排序)

    1052 Linked List Sorting(排序) 思路: s t r u c t + struct+ struct+排序. 坑点: 1.答案只包含从原链表中头结点开始的结点,有点结点不在原链表 ...

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

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A linked list consists of a series of structures, which are not n ...

  6. 1052. Linked List Sorting (25)

    考察链表的知识,以及排序 题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052 #include<iostream> #inclu ...

  7. 1052 Linked List Sorting 分数 25

    题目描述 A linked list consists of a series of structures, which are not necessarily adjacent in memory. ...

  8. PAT 1052. Linked List Sorting

    这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发.估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过 #include <iost ...

  9. 1052 Linked List Sorting (25 分)【难度: 一般 / 知识点: 链表】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 哈希表模拟链表. #include<b ...

最新文章

  1. configure,pkg-config和PKG_CONFIG_PATH
  2. @RequestMapping的使用
  3. 为什么 Linux 默认页大小是 4KB
  4. idea导入gradle项目ctrl无法定位问题
  5. 南方cass提取坐标生成表格_南方cass方格网角点坐标及高程的识别与导出
  6. 保护模式下的80386及其编程03:保护虚拟地址方式
  7. 计算机 交换机连接设置方法,怎么用串口线连接电脑和交换机 连接步骤教程
  8. MacBook 管理员账户无法删除 解决方法
  9. c语言中的EOF是什么意思
  10. 用java设计一个矩形类_6-1 设计一个矩形类Rectangle (10分)
  11. 微信公众平台开发(PHP)(二) 微信公众平台示例代码分析
  12. 【拜小白的机器学习】2-机器学习的种类与基本术语概念
  13. 从JavaScript到TypeScript,Pt。 IIB:使用类,接口和混合器进行设计
  14. netcat工具简单使用
  15. Visual Saliency Transformer 读后感
  16. 万物互联 | 你的企业可能需要这样一个物联网平台
  17. 离线报表之五大看板主题需求分析(SQL版)
  18. ASP.NET会员管理系统
  19. 单体内置对象_js基础-单体内置对象(Global、Math)
  20. Redis缓存击穿,缓存穿透,缓存雪崩,附解决方案

热门文章

  1. 【React Native】iOS原生导航跳转RN页面
  2. APP程序内部打开某个APP的AppStore页面
  3. 异步系统接口调用流程图
  4. 4-1 ADO.NET简介
  5. C++vector迭代器失效的问题
  6. BIZTALK项目中WEB引用WEBSERVICES服务时候报错
  7. apache 2.4.12 + tomcat 7.0.61 + jk connectors 1.2.40实现tomcat负载均衡集群
  8. 更新 FrameWork
  9. 【Elasticsearch 5.6.12 源码】——【3】启动过程分析(下)...
  10. 展望2018:WebRTC大规模商用元年