02-1. Reversing Linked List (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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)详细题解的更多相关文章

  1. 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 ...

  2. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  3. 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 ...

  4. PAT甲题题解-1074. Reversing Linked List (25)-求反向链表

    题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...

  5. 数据结构练习 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 ...

  6. 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 ...

  7. 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 ...

  8. 浙大数据结构课后习题 练习二 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 ...

  9. 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 ...

随机推荐

  1. SharePoint 2016 每天预热脚本介绍

    使用SharePoint的朋友们应该知道,SharePoint每天夜里有自动回收的机制,使环境每天把占用的内存都释放出来,以确保不会累计占用过多内存导致服务器崩溃. 我们可以打开IIS,选中我们的应用 ...

  2. 【Android Developers Training】 48. 轻松拍摄照片

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  3. 解决VMware虚拟机不能上网的问题

    这是其中一种情况,因为使用360等加速的时候把VMware 的DHCP等服务关闭了,重新启动服务即可,方法是WIN+R 输入cmd,在doc输入services.ms打开服务,找到 把这些都启动即可

  4. Java虚拟机面试重点-------------内存分配和回收策略

    1 对象优先分配在Eden区 对象优先在Eden进行分配,大多数情况下,对象在新生代Eden区进行分配.当Eden区没有足够的空间进行分配时,虚拟机会发起一次Minor GC. 新生代GC(Ninor ...

  5. VB6之写注册表

    难免会遇到写注册表的情况,写了个实用点的RegWrite函数.为了减少代码量,用WScript.Shell取代了API来实现. 使用方式就在注释中了,就不再过多解释了.PS:注释比实现代码要丰富多了, ...

  6. bcp和load table

    使用BCP和LOAD TABLE联合完成Sybase IQ 的数据导出和导入工作.说明: 表(视图)GN_TEST只有两个字段,TIMEID和MSISDN, 导出时我用'|'作为字段分隔符,'& ...

  7. Mac之OS系统下搭建JavaEE环境 <二> 之Tomcat 的安装配置

    二.Tomcat的安装与配置 1.下载Tomcat 找到Tomcat的官网 百度搜索Tomcat 点击下载即可 下载网址:http://tomcat.apache.org/download-80.cg ...

  8. Go学习笔记(一)Let's 干吧

    加 Golang学习 QQ群共同学习进步成家立业 ^-^ 群号:96933959 简介     Go是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言.为了方便搜索和识 ...

  9. git分支的使用

    本文章假定你已经接触了一些git的基本概念和基本的操作知识 这里先贴出关于分支的一些常用命令 git branch /*查看所有分支*/git branch <branch-name> / ...

  10. PHP中的数据结构

    PHP7以上才能安装和使用数据结构,安装比较简单: 1. 运行命令 pecl install ds 2. 在php.ini中添加 extension=ds.so 3. 重启PHP或重载配置  Coll ...