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 ...
随机推荐
- TPYBoard自制微信远程智能温湿度计
智能时代一夜间什么都能远程了.创业者想着如何做智能产品,如何做远程控制.DIY爱好者也想着如何自制各种奇妙的工具.这里和大家一起学习制作一款廉价的智能温湿度计.说它廉价是因为共计花费不过40元,说它智 ...
- [leetcode-605-Can Place Flowers]
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 【Android Developers Training】 98. 获取联系人列表
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- VB6之阴影图层
要是能创建半透明的刷子就好了,就不必像这样以图层的方式实现透明阴影效果. 代码: 'code by lichmama@cnblogs.com '绘制阴影图层 Private Declare Funct ...
- Scrapy爬虫实例教程(二)---数据存入MySQL
书接上回 实例教程(一) 本文将详细描述使用scrapy爬去左岸读书所有文章并存入本地MySql数据库中,文中所有操作都是建立在scrapy已经配置完毕,并且系统中已经安装了Mysql数据库(有权限操 ...
- [基础架构]PeopleSoft Application Server 重要文件说明
我们都知道PeopleSoft是由几个不同的服务组成的,他们在PeopleSoft体系结构中扮演着自己的角色.这些服务具有不同的文件结构并包含重要的可执行文件和配置文件. 以下是Peoplesoft体 ...
- Java 容器在实际项目开发中应用
前言:在java开发中我们离不开集合数组等,在java中有个专有名词:"容器" ,下面会结合Thinking in Java的知识和实际开发中业务场景讲述一下容器在Web项目中的用 ...
- Japanese Learning - Words and Sentences 1
1. いらっしゃいませ.何名さまですか. 二人です. タバコをお吸いになりますか. かしこまりました.少々お待ちください. お勘定お願いします. ご一緒でよろしいでしょうか. 別々にお願いします. 2 ...
- Scala关于软件的安装
1.安装JDK 因为很多系统都会预装java,所以我们首先要测试Java版本.这里我们要求要在1.8以上.在终端输入java -version来测试版本 Windows 关于Windows,我们首先要 ...
- cocos2dx 图片压缩工具 推荐
随着版本的第二次迭代,包体越来越大;随之而来的也就是下载量会有所影响;毕竟每增大一点包体大小,下载的玩家就会损失一小批; 但是我们的图片都经过美术压缩打包,依然包体不小; 最后发现是因为把图片都打包成 ...