题目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. !HDU 1574 RP-dp-(重点在状态确定)

    题意:有n件事.每件事若发生有两种情况.添加RP为a,可是收益会降低c:降低R为a,收益会添加c. 每件事可以发生的前提是小于等于或者大于等于门限值b.求最大收益. 分析:这题我没找到状态,所以就不会 ...

  2. golang ---tcmalloc浅析

    总体结构 在tcmalloc内存管理的体系之中,一共有三个层次:ThreadCache.CentralCache.PageHeap,如上图所示.分配内存和释放内存的时候都是按从前到后的顺序,在各个层次 ...

  3. Effective Java 第三版——36. 使用EnumSet替代位属性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. 第三部分:Android 应用程序接口指南---第二节:UI---第十一章 样式和主题

    第11章 样式和主题 style是用于指定View或window的外观和格式的一系列属性的集合.style可以指定高(height).填补(padding).字体颜色.字体大小.背景颜色等等属性.st ...

  5. 【Spark 深入学习 04】再说Spark底层运行机制

    本节内容 · spark底层执行机制 · 细说RDD构建过程 · Job Stage的划分算法 · Task最佳计算位置算法 一.spark底层执行机制 对于Spark底层的运行原理,找到了一副很好的 ...

  6. 【iCore4 双核心板_FPGA】例程八:乘法器实验——乘法器使用

    实验现象: 程序运行时,绿色led闪烁(目前,具体的乘法器调用请参考iCore3乘法器例程) 核心代码: module multiplier_ctrl( input clk_25m, input rs ...

  7. 看雪CTF第十五题

    1.直接运行起来,再用OD附加 在此处luajit加载并调用main函数 004021C7 E8 64FE0000 call CrackMe. ; luaL_newstate 004021CC 8BF ...

  8. android开发(48) Android Snackbar 的使用

    Snackbar 类似toast,用于向 用户展示信息,和用户交互,它能够显示一个 按钮 获得用户的操作.它的特点如下: 作为android.support.design.widget.Coordin ...

  9. hdoj:2042

    #include <iostream> using namespace std; int main() { int n,a; while (cin >> n) { while ...

  10. recyclerView插入(add)和删除(remove)item后,item错乱,重复,覆盖在原recyclerView上

    项目用到,实现一个recyclerView列表的item翻转动效,翻转的同时会将指定item置顶. (比如交换AB位置,A在0位置,指定的item B 在 i 位置) 原始使用的是插入B到0位置,然后 ...