题目https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464

题意:

给定一些内存中的节点的地址,值以及下一节点所在地址。

要求对给定的头指针表示的链表进行排序。

思路:

PAT的题目小细节真的很多啊。这道题要注意有可能有的节点是不在头指针链成的链表里的。

所以要先遍历一次链表,然后再排序。

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, head;
struct node{
int key;
int nxt;
}l[];
struct num{
int add;
int key;
}tmp[]; bool cmp(num a, num b)
{
return a.key < b.key;
} int main()
{
scanf("%d%d", &n, &head);
for(int i = ; i < n; i++){
int a;
scanf("%d", &a);
scanf("%d%d", &l[a].key, &l[a].nxt);
} int now = head;
int cnt = ;
while(now != -){
tmp[cnt].add = now;
tmp[cnt].key = l[now].key;
now = l[now].nxt;
cnt++;
}
sort(tmp, tmp + cnt, cmp);
if(cnt == ){
printf("0 -1\n");
}
else{
printf("%d %05d\n", cnt, tmp[].add);
for(int i = ; i < cnt - ; i++){
printf("%05d %d %05d\n", tmp[i].add, tmp[i].key, tmp[i + ].add);
}
printf("%05d %d -1\n", tmp[cnt - ].add, tmp[cnt - ].key);
} return ;
}

PAT甲级1052 Linked List Sorting的更多相关文章

  1. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  2. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  3. PAT甲级——A1052 Linked List Sorting

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

  4. PAT Advanced 1052 Linked List Sorting (25) [链表]

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

  5. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  6. PAT 1052 Linked List Sorting [一般]

    1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...

  7. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  8. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  9. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

随机推荐

  1. 在Linux平台上搭建EasyDarwin,编译代码并简单部署

    測试环境: Ubuntu gcc / g++ 从https://github.com/EasyDarwin/EasyDarwin下载代码 1.编译 第一步:进入源码文件夹下 cd ./EasyDarw ...

  2. funny

    var life = { "work_hard","have_fun","make_history" };

  3. 【Babble】批量学习与增量学习、稳定性与可塑性矛盾的乱想

    一.开场白 做机器学习的对这几个词应该比较熟悉了. 最好是拿到全部数据,那就模型慢慢选,参数慢慢调,一轮一轮迭代,总能取得不错效果. 但是面对新来数据,怎么能利用已经训练好的模型,把新的信息加进去? ...

  4. [Aaronyang] 写给自己的WPF4.5 笔记10[层次数据需求处理,TreeView绿色文章1/4]

     我的文章一定要做到对读者负责,否则就是失败的文章  ---------   www.ayjs.net    aaronyang技术分享 AY留言: 文章根据难易,我根据游戏的规则进行了分色,希望读者 ...

  5. C#:CeF遇到的问题

    2.CSharp与JS交互问题: 1)在 继承CefRenderProcessHandler的子类中重载OnWebKitInitialized()函数,注册JS类 2)在 继承CefApp的子类中创建 ...

  6. 何为编码 GBK 和 UTF8编码?GBK,GB2312与区位码有何关系?

    何为GBK,何为GB2312,与区位码有何渊源? 区位码是早些年(1980)中国制定的一个编码标准,如果有玩过小霸王学习机的话,应该会记得有个叫做“区位”的输入法(没记错的话是按F4选择).就是打四个 ...

  7. layui table 根据条件改变更换表格颜色 高亮显示 数据筛选

    请问想让当layui表格的某个字段符合某个条件的时候,让该行变颜色.这样可以实现么. layui数据表格怎么更换表格颜色 layui表格 通过判断某一行中的某一列的值进行设置这一行的颜色 LayUI之 ...

  8. etcd 集群运维实践

    etcd 是 Kubernetes 集群的数据核心,最严重的情况是,当 etcd 出问题彻底无法恢复的时候,解决问题的办法可能只有重新搭建一个环境.因此围绕 etcd 相关的运维知识就比较重要,etc ...

  9. 按enter键触发登录事件

    $(document).keydown(function(event){ if(event.keyCode==13){ $(".submit").click(); } });

  10. Python3自定义http/https请求拦截mitmproxy脚本

    [本文出自天外归云的博客园] 脚本内容 代码如下: from mitmproxy import http, ctx from multiprocessing import Lock class Fil ...