PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key
and a Next
pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (<) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address
is the address of the node in memory, Key
is an integer in [−], and Next
is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
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
题意:
给出一个链表,将链表排序,然后把链表上的结点按照data值的从小到大顺序输出。
题解:
数组模拟链表
先后使用两种不同的存储方式。
注意:
1.不一定所有的节点都在链表里(一开始没仔细读题,只得了21分)
2.链表中没有元素时候第一行不输出,第二行输出 0 -1。
AC代码:
#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int n;
struct node{
int k;
int next;
}a[];
struct node2{
int k;
int add;
}b[];
int x;
bool cmp(node2 x,node2 y){
return x.k<y.k;
}
int main(){
int root;
cin>>n>>root;
a[root].next=-;
for(int i=;i<=n;i++){
cin>>x;
cin>>a[x].k>>a[x].next;//这样存储
}
int num=;
if(a[root].next==-){//没有一个在链表里
printf("%d %d",,-);
return ;
}
while(a[root].next!=-){//排序的节点必须都在链表里
b[++num].k=a[root].k;//换一种存储节点的方式,按顺序存储
b[num].add=root;
root=a[root].next;
}
b[++num].k=a[root].k;
b[num].add=root;
sort(b+,b++num,cmp);
printf("%d %05d\n",num,b[].add);
for(int i=;i<=num;i++){
printf("%05d %d ",b[i].add,b[i].k);
if(i!=num) printf("%05d\n",b[i+].add);
else cout<<"-1";
}
return ;
}
柳神的题解写的更好,学习一下:
分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表,将在链表中的结点的flag标记为true,并且统计cnt(有效结点的个数)。(因为有的结点根本不在链表中)
然后将链表进行排序,如果flag == false就把他们移动到后面(即:reuturn a.flag > b.flag),最后只输出前cnt个链表的信息~
#include <algorithm>
using namespace std;
struct NODE {
int address, key, next;
bool flag;
}node[];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
}
int main() {
int n, cnt = , s, a, b, c;
scanf("%d%d", &n, &s);
for(int i = ; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
node[a] = {a, b, c, false};
}
for(int i = s; i != -; i = node[i].next) {
node[i].flag = true;
cnt++;
}
if(cnt == ) {
printf("0 -1");
} else {
sort(node, node + , cmp1);
printf("%d %05d\n", cnt, node[].address);
for(int i = ; i < cnt; i++) {
printf("%05d %d ", node[i].address, node[i].key);
if(i != cnt - )
printf("%05d\n", node[i + ].address);
else
printf("-1\n");
}
}
return ;
}
PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)的更多相关文章
- 【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 ...
- PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)
1017 Queueing at Bank (25 分) Suppose a bank has K windows open for service. There is a yellow line ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- 1052 Linked List Sorting (25分)
题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- 【PAT甲级】1109 Group Photo (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
随机推荐
- kafka读书笔记《kafka并不难学》
======第一章 1 在高并发场景,如大量插入.更新数据库会导致锁表,导致连接数过多的异常,此时需要消息队列来缓冲一下.消息队列通过异步处理请求来缓解压力 2 消息队列采用异步通信机制消息队列拥有先 ...
- 使用JSP/Servlet技术开发新闻发布系统---JSP数据交互(二)
JSP内置对象application application对象 JSP常用的内置对象 对象的作用域 作用的分类 对象的作用域 page作用域 实例 //页面1 <% String name = ...
- sql server 很好的基础练习 <学生表-老师表-课程表-选课表>
表结构 --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称CourseName. ...
- 二十八. Ceph概述 部署Ceph集群 Ceph块存储
client :192.168.4.10 node1 :192.168.4.11 ndoe2 :192.168.4.12 node3 :192.168.4.13 1.实验环境 准备四台KVM虚 ...
- exam8.29
咕了好几篇后... 我终于开始重新写了 T1: 不会,没思路,暴搜还可能会(一开始我以为暴搜时间复杂度为$\Theta (mn ^ k)$) 于是码出了暴搜... 跑一遍$(4,4,5)$,然后... ...
- 爬虫(十七):scrapy分布式原理
一:scrapy工作流程 scrapy单机架构: 单主机爬虫架构: 分布式爬虫架构: 这里重要的就是我的队列通过什么维护?这里一般我们通过Redis为维护,Redis,非关系型数据库,Key-Valu ...
- 【概率论】5-6:正态分布(The Normal Distributions Part I)
title: [概率论]5-6:正态分布(The Normal Distributions Part I) categories: - Mathematic - Probability keyword ...
- 认识WebStorm-小程序框架wepy
WebStorm是一个功能强大的IDE,适用于JavaScript开发,适合使用Node.js进行复杂的客户端开发和服务器端开发. WebStorm具有对JavaScript,HTML, CSS及其现 ...
- (转)centos7安装telnet服务
借鉴:https://www.cnblogs.com/daipenglin/p/4934572.html 阅读目录 1 CentOS7.0 telnet-server 启动的问题 场景:在进行Teln ...
- [WEB安全]IIS-PUT漏洞
目录 0x00 IIS简介 0x01 Put漏洞造成原因 0x02 实验环境搭建 0x03 需要用到的工具 0x04 IIS-PUT漏洞演示实战 0x05 常见请求协议 0x06 漏洞修复建议 0x0 ...