PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
这题用二维数组高维代表首地址啊address,低维[address][0]储存data,[address][1]则储存next,牺牲空间换时间。
再使用维数组list进行reverse.
int list[];
int node[][]; int st,num,r;
cin>>st>>num>>r;
int address,data,next,i;
for(i=;i<num;i++)
{
cin>>address>>data>>next;
node[address][]=data;
node[address][]=next;
}
先输入node值。如图
| address | 00000 | 00100 | 12309 | 33218 | 68237 | 99999 |
| data | 4 | 1 | 2 | 3 | 6 | 5 |
| next | 99999 | 12309 | 33218 | 00000 | -1 | 68237 |
值得说明的是:不需要按照顺序输入,到数组中后自然会对应数组的下标值即address。
因此上表中按照数组顺序排序,无初始值的数组下标省略不列。
下面对list赋address.
int m=,n=st;
while(n!=-)
{
list[m++]=n;
n=node[n][];
}
| list | 0 | 1 | 2 | 3 | 4 | 5 |
| address | 00100 | 12309 | 33218 | 00000 | 99999 | 68237 |
其中list[0]储存的是st的address,list[1]储存的是st->next的地址,以此类推。直到遇到address为-1.
i=;
while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}
进行反转,使用algorithm的reverse函数。
for (i = ; i < m-; i++)
{
printf("%05d %d %05d\n", list[i], node[list[i]][], list[i+]);
}
printf("%05d %d -1\n", list[i], node[list[i]][]);
最后进行输出,注意的是以int形式储存的,如address中的00000实际储存位置是0,故输出时注意是要用五位数输出。
完整AC代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
int list[];
int node[][];
int main()
{
freopen("F:\\in1.txt","r",stdin);
int st,num,r;
cin>>st>>num>>r;
int address,data,next,i;
for(i=;i<num;i++)
{
cin>>address>>data>>next;
node[address][]=data;
node[address][]=next;
}
int m=,n=st;
while(n!=-)
{
list[m++]=n;
n=node[n][];
}
i=;
while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}
for (i = ; i < m-; i++)
{
printf("%05d %d %05d\n", list[i], node[list[i]][], list[i+]);
}
printf("%05d %d -1\n", list[i], node[list[i]][]);
}
PAT1074 Reversing Linked List (25)详细题解的更多相关文章
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- PAT甲题题解-1074. Reversing Linked List (25)-求反向链表
题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...
- 数据结构练习 02-线性结构2. 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 ...
- PAT 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 ...
- 02-线性结构3 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 ...
- 浙大数据结构课后习题 练习二 7-2 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 ...
- PAT Advanced 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 e ...
随机推荐
- jquery元素是否可见(隐藏)
var temp1=$(".view_hidden").is(":visible");//是否可见 var temp2=$(".elem_id&quo ...
- iOS多线程开发之NSOperation - 快上车,没时间解释了!
一.什么是NSOperation? NSOperation是苹果提供的一套多线程解决方案.实际上NSOperation是基于GCD更高一层的封装,但是比GCD更加的面向对象.代码可读性更高.可控性更强 ...
- Android创建窗口(一)创建应用窗口
所谓的窗口(Window)就是一个显示在手机屏幕上可视化视图的一片区域.在Android中窗口是一个抽象的概念,每一个Activity就对应着一个窗口,而所有的窗口都是由视图(View)来呈现,而我们 ...
- Gym 100952 D. Time to go back(杨辉三角形)
D - Time to go back Gym - 100952D http://codeforces.com/gym/100952/problem/D D. Time to go back time ...
- 用js写一个回车键盘事件
用js来监听键盘事件,代码如下: <script type="text/javascript" language=JavaScript charset="UTF-8 ...
- 在Linux下安装Oracle12c
其实,对于oracle数据库和oracle实例的安装,借用图形化安装还是比较容易的,只是有个别地方需要特别注意外,其余的默认安装即可: 1.安装前的准备: 启动SSH工具: 先启动倒数第三个(想用图像 ...
- Linux服务器学习(一)
一.首先连接服务器 下载一个windows下连接linux的ssh工具,我这里用的putty.一次填入HostName(主机名,可以是服务器域名也可以是对应的ip).Port(端口号默认为22).Co ...
- 关于阻止PROE联网的一些想法!
前言:商业上使用盗版proe会被官方警告,官方是通过proe软件联网来获取你的ip地址,那他是怎么知道你是商业用途而不是个人(一般不会对个人的盗版行为进行警告)?这不难,应该是通过检测同一ip下有多台 ...
- 【转载】BAT 批处理脚本教程
来源:http://www.cnblogs.com/glaivelee/archive/2009/10/07/1578737.html BAT 批处理脚本 教程 第一章 批处理基础第一节 常用批处 ...
- maven-编译速度优化
故障描述: 公司搭建了一个新jenkins持续集成环境,jenkins构建job时间越来越长. 原因分析: 系统CPU限制:判断依据,构建中查看日志 tail -f /var/log/messages ...