1052 Linked List Sorting
题意:链表排序
思路:题目本身并不难,但是这题仔细读题很重要。原题中有一句话,"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",它没有明确表示这个N就是链表的结点个数,而是内存中含有的结点个数(大家可以相比较另外几道链表的题目,看看题目描述的细微差别)。因此,在读入数据后,要从头结点开始遍历一遍链表,把链表上的结点提取出来,因为,输入的链表有可能是不属于这个链表的干扰结点。有几个坑需要注意——
1.输入的结点并不全是链表的结点,因此在正式排序前应先提取出有效结点;
2.有可能在第1步完成后,有效结点为0,故输出 0 -1。
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
;
struct Node{
int data;
int curr,next;
}LinkList[N],node[N];
bool cmp(Node a,Node b){
return a.data<b.data;
}
int main()
{
int n,head;
scanf("%d%d",&n,&head);
int curr,data,next;
;i<n;i++){
scanf("%d%d%d",&curr,&data,&next);
LinkList[curr].data=data;
LinkList[curr].curr=curr;
LinkList[curr].next=next;
} //提取有效结点
;
){
node[cnt++]=LinkList[head];
head=LinkList[head].next;
}
sort(node,node+cnt,cmp);
){//空链表的情况
printf("0 -1\n");
;
}
printf(].curr);
;i<cnt;i++){//注意遍历个数cnt,不是n
printf("%05d %d ",node[i].curr,node[i].data);
) printf(].curr);
else printf("-1\n");
}
;
}
常见的错误写法:
#include <cstdio>
#include <algorithm>
using namespace std;
;
struct Node{
int data;
int curr,next;
}LinkList[N];
bool cmp(Node a,Node b){
return a.data<b.data;
}
int main()
{
//freopen("pat.txt","r",stdin);
int n,head;
scanf("%d%d",&n,&head);
int curr,key,next;
;i<n;i++){
scanf("%d%d%d",&curr,&key,&next);
LinkList[i].data=key;
LinkList[i].curr=curr;
LinkList[i].next=next;
}
sort(LinkList,LinkList+n,cmp);
printf(].curr);
;i<n;i++){
printf("%05d %d ",LinkList[i].curr,LinkList[i].data);
) printf(].curr);
else printf("-1\n");
}
;
}
1052 Linked List Sorting的更多相关文章
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- 【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 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT甲级1052 Linked List Sorting
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...
随机推荐
- 拖拽窗口的实现-JQuery实现;
主要是距离的掌握 如图,原始位置和当前位置. 对于当前位置:想要求得left值b',需要b'=a'-c; 其中,a’= ev.pageX;就是指针当前距离文档左边的距离: 同时,可以发现c在拖拽过程中 ...
- 使用maven创建项目时报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" wa ...
- ng2 学习笔记(一)
ng2发布了一段时间,最近才开始着手学习一下,ng2可以说变化海是比较大的,现在写一些学习过程中要注意的点,新手可以参考,大神可以指导: 按照文档来吧: 1.快速开始:没什么可说的,直接上git 克隆 ...
- iscroll.js的简单使用方法(总结)
iscroll.js的简单使用方法(总结) 一.总结 一句话总结:Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化. 最佳的HTML结构如下: <div id="wrap ...
- MySQL for Mac 5.7.x 版本忘记密码修改root密码
1.安装mysql 2.命名别名: vim ~/.bash_profile alias mysql=/usr/local/mysql/bin/mysql alias mysqladmin=/usr/l ...
- netty的异常分析 IllegalReferenceCountException refCnt: 0
netty的异常 IllegalReferenceCountException refCnt: 0 这是因为Netty有引用计数器的原因,自从Netty 4开始,对象的生命周期由它们的引用计数(ref ...
- spring3: Bean的作用域
3.4 Bean的作用域 什么是作用域呢?即“scope”,在面向对象程序设计中一般指对象或变量之间的可见范围.而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围. ...
- Android 之Navicat for SQLite 数据库介绍
Navicat for SQLite 是一套专为SQLite 设计的强大数据库管理及开发工具 Navicat for SQLite 是一套专为SQLite 设计的强大数据库管理及开发工具.它可以 ...
- [置顶]
Kubernetes1.7新特性:支持绕过docker,直接通过containerd管理容器
背景情况 从Docker1.11版本开始,Docker依赖于containerd和runC来管理容器,containerd是控制runC的后台程序,runC是Docker公司按照OCI标准规范编写的一 ...
- 基于用户登陆的struts2中action的分类详解
在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式. 1.继承 Actio ...