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

知识点:链表

//1052 Linked List Sorting(25 分)
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;
//步骤1:定义静态链表,其中结点性质bool型变量flag定义,表示为结点在链表中是否出现
//      flag为false表示无效结点
struct Node { //定义静态链表int address, data, next;bool flag;//结点是否在链表上
}node[maxn];//步骤3:对节点进行排序
bool cmp(Node a, Node b) {if (a.flag == false || b.flag == false) {return a.flag > b.flag;}else {return a.data < b.data;}
}int main() {//步骤2:初始化for (int i = 0;i < maxn;i++) {node[i].flag = false;}int n, begin, address;scanf_s("%d%d", &n, &begin);for (int i = 0;i < n;i++) {scanf_s("%d", &address);scanf_s("%d%d", &node[address].data, &node[address].next);node[address].address = address;}int count = 0, p = begin;//步骤3:枚举链表,对flag进行标记,同时计数有效结点个数while (p != -1) {node[p].flag = true;count++;p = node[p].next;}if (count == 0) {  //特判:新链表中没有结点时输出 0 -1printf("0 -1");}else {//步骤4:筛选有效结点,并按data从小到大排序sort(node, node + maxn, cmp);//步骤5:输出结果//防止-1被%05d化,提前判断printf("%d %05d\n", count, node[0].address);for (int i = 0;i < count;i++) {if (i != count - 1) {printf("%05 %d %05d\n", node[i].address, node[i].data, node[i + 1].address);}else {printf("%05d %d -1\n", node[i].address, node[i].data);}}}return 0;
}

1052 Linked List Sorting (25 分)相关推荐

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

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

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

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

  3. 1052 Linked List Sorting (25分)

    和 1074一样的方法,只需要管自己的结点就可以了,不需要处理next, 注意最后一个测试点,测试数据最终结果 为0个结点,只需要输出 0 -1 附本人AC代码: #include<iostre ...

  4. 1052. Linked List Sorting (25)

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

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

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

  6. 1052. Linked List Sorting

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

  7. 1052 Linked List Sorting(排序)

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

  8. PAT甲级1052:Linked List Sorting (25)

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

  9. 1052 Linked List Sorting 分数 25

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

最新文章

  1. Python面对对象编程——结合面试谈谈封装、继承、多态,相关习题详解
  2. HTTP调试工具:Fiddler 介绍二
  3. Winsock属性 方法介绍
  4. 纯CSS实现3D照片墙
  5. linux查看注册表信息,linux下登录档及其查看方法
  6. 控制台输出HelloWorld案例
  7. 【ClickHouse】查看数据库容量和表大小的方法(system.parts各种操作方法)
  8. java模板设计学习
  9. SQL:having和where的区别
  10. 实战突击:PHP项目开发案例整合(第2版)
  11. Webrtc中stun和turn的理解
  12. tensor.detach() 和 tensor.data 的区别
  13. 对象释放函数kmem_cache_free核心函数slab_free的实现详解
  14. Linux NAT基本流程与实现技巧
  15. Docker安装(有网环境下) 最新版docker-ce安装教程
  16. Python-基于request豆瓣电影票房信息爬取,简单粗暴
  17. Spring Cache使用Redisson分布式锁解决缓存击穿问题
  18. 如何官网下载Eclipse历史版本
  19. python中对字典的循环遍历的方式
  20. WinRAR显示离购买许可只剩xx天

热门文章

  1. Java实现 LeetCode 312 戳气球
  2. MATLAB从2018到2019版本-randint函数,modem函数的变更
  3. 【kaggle】Spaceship Titanic - 预测哪些乘客被运送到另一个维度【CatBoost - 10%】
  4. java中keyset_Java中的提供程序keySet()方法
  5. Ubuntu安装宝塔部分展示
  6. 波浪下划线怎么设置_下划线双波浪怎么设置
  7. 原生ajax(常见的http状态码/同源-跨域)
  8. 创意产业如何从区块链中受益?
  9. 调用iframe嵌套页面的方法
  10. sourceTree连接阿里云code