链表里的元素可能没有
思路:使用静态链表
注意:存在给出的地址不在链表上,此外,存在链表没有任何节点。所以这个故事告诉我们,多想一下出题人要求我们输出内容的必要性,一般情况,出题人不会脑残让我们读入什么,就输出什么。

版本1

PAT测试点

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100006;
struct node{int key,add,next;bool flag;
}Node[maxn];bool cmp(node a, node b){if(a.flag==false || b.flag==false){return a.flag > b.flag;}else{return a.key<b.key;}
}
int main(){int n,head,add;for(int i=0;i<maxn;i++){  //初始化 Node[i].flag=false;}scanf("%d%d",&n,&head);for(int i=0;i<n;i++){scanf("%d",&add);Node[add].add=add;scanf("%d %d",&Node[add].key,&Node[add].next);}int cnt=0,p=head;while(p!=-1){Node[p].flag=true;  //在链表上cnt++;p=Node[p].next; }if(cnt==0){ //链表没有任何节点时 printf("0 -1\n"); return 0;}sort(Node,Node+maxn,cmp);printf("%d %05d\n",cnt,Node[0].add);for(int i=0;i<cnt-1;i++){printf("%05d %d %05d\n",Node[i].add,Node[i].key,Node[i+1].add);}printf("%05d %d -1\n",Node[cnt-1].add,Node[cnt-1].key);return 0;
}

版本2

牛客网测试点

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
struct node{int val, tag, next, add;
}E[maxn];
bool cmp(node a, node b){if(a.tag != b.tag) return a.tag > b.tag;else return a.val < b.val;
}
int main(){int n, p, add, k = 0;cin >> n >> p;for(int i = 0; i < n; i++){cin >>add;E[add].add = add;cin>>E[add].val>>E[add].next;}while(p != -1){E[p].tag = 1;p = E[p].next;k++;}sort(E, E + maxn, cmp);if(k == 0){printf("0 -1\n");return 0;}printf("%d %05d\n", k, E[0].add);for(int i = 0; i < k; i++){printf("%05d %d ",E[i].add, E[i].val);if(i != k-1) printf("%05d\n", E[i+1].add);else printf("-1\n");}return 0;
}

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

  1. 1052 Linked List Sorting (25 分)

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

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

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

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

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

  4. 1052 Linked List Sorting (25分)

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

  5. 【简洁代码】1028 List Sorting (25 分)_26行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Excel can sort records according to any column. Now you are suppo ...

  6. 【PAT甲级 - 1028】List Sorting (25分)(模拟,排序)

    题干: Excel can sort records according to any column. Now you are supposed to imitate this function. I ...

  7. PAT-A 1074 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  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)

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

最新文章

  1. OpenCV+python:模糊操作
  2. 小熊电器、九阳、苏泊尔们的“颜价比”被外卖小哥“打回原形”
  3. 从 6 万用户评论中,选出这 23 门全世界最好的 CS 免费课
  4. 一份完整的问卷模板_如何写出一份优秀的个人简历?
  5. 开发中遇到的Cause: java.sql.SQLException: connection holder is null的异常
  6. 四部门对蚂蚁集团提出整改要求,蚂蚁集团:已立即着手制定整改方案
  7. Newtonsoft 六个超简单又实用的特性,值得一试 【上篇】
  8. Python——装饰器的学习笔记
  9. p_sequencer 再理解
  10. 关于MSDTC - 与基础事务管理器的通信失败 错误解决的小备忘
  11. 项目管理PMP高频考点复习资料(2-2)
  12. 苹果cms播放器html,苹果cms 全局播放器dplayer带后台Dplayer播放器苹果CMSV10插件
  13. 华为服务器控制口地址修改,服务器修改管理口地址
  14. 有功功率,无功功率,视在功率定义
  15. 富文本编辑器mavon-editor文章回显
  16. Virtualbox主机和虚拟机之间文件夹共享及双向拷贝
  17. 二年级期末计算机基础试题答案,小学二年级语文期末考试卷
  18. 微信开发者工具如何打开企业微信小程序
  19. python enumerate_Python enumerate() 函数
  20. 2023计算机毕业设计SSM最新选题之java健身俱乐部5jz0z

热门文章

  1. 前端 环形统计_10款jQuery实现的环形百分比图表插件
  2. 基于simulink的PN码相关峰同步仿真
  3. ajax有哪些常用参数?ajax参数介绍
  4. WIFI知识 - MCS简介
  5. CLH、MCS锁的原理及实现
  6. 无线路由实战(一):增强无线wifi信号和加速wifi的十一种方法
  7. 上海亚商投顾: 沪指冲高回落 高景气赛道股回归
  8. 从给定的大图中切出指定矩形区域的小图
  9. 一次学神and农夫and“i春秋”的心理交锋
  10. html5基础考试选择题,HTML5 基础测试题