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. url地址栏拼接参数写法

    <script> function jiedan_do(elm){ var id=$(elm).attr("a"); window.location="__U ...

  2. js实现类似iphone的秒表-添加平均数功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. NavigationView的头部的事件监听

    现在App的UI设计中Drawerlayout+NavigationView是一个比较常用的设计了,而以前我一般只是在Navigation中的menu(即下部的item中)添加事件监听,而今天碰到一个 ...

  4. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  5. 修改ElementUI源码实践

    提要 Vue2.0+Vuex+ElementUI是现在很多项目都在使用的BS软件的开发组合. Vue相较于Angular具有学习成本低,上手快以及组件轻量化的特点:相较于React,其官方提供的很多指 ...

  6. js几秒以后倒计时跳转示例

    代码如下: <html> <head> <title>出错啦~~~</title> <link href="css/login1.css ...

  7. Interlocked原子函数陷阱

    一.问题 windows api函数中提供了InterlockedExchange.InterlockedDecrement, InterlockedIncrement, ExInterlockedA ...

  8. 浅谈javascript中的call与apply方法

    call方法与apply方法都是为了改变函数体内部this的指向. call方法与apply方法,这二者的作用完全一样,只是接受参数的方式不太一样. apply()方法: Function.apply ...

  9. 51nod_1100:斜率最大

    题目链接 斜率最大点对横坐标必相邻 #include <bits/stdc++.h> using namespace std; ; struct point { int x, y, pos ...

  10. 支付宝LR集群压测报告

    支付宝压力测试报告 时间:2016-03-23                                             测试人员:XXX 目录 支付宝压力测试报告 1 目录 1 一   ...