PAT甲级——A1074 Reversing Linked List
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 (≤) which is the total number of nodes, and a positive K (≤) 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
算法设计:
由于所给的地址是5位非负整数,可以定义两个维度为100005的一维数组data、Next,负责储存数据域和下一个地址
定义一个vector<int>listAddress,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到listAddress中。
按要求对listAddress数组进行翻转,可以利用c语言库里的reverse函数
按格式要求进行结果输出
注意点:
(1)题目给出的结点中可能有不在链表中的无效结点
(2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转
(3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出
//s首先说明一下PAT的常见陷阱,就是给出的数据未必是一条链表,可能是多条,
//所以首先从输入的数据的中找到那条链表
//巨简单,使用vector反转就行了
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int head, N, K;
int nodes[], nexts[];
vector<int>list;
int main()
{
cin >> head >> N >> K;
int adrr, data, next, temp = head;
for (int i = ; i < N; ++i)
{
cin >> adrr >> data >> next;
nodes[adrr] = data;
nexts[adrr] = next;
}
while (temp != -)//找出这条链表
{
list.push_back(temp);
temp = nexts[temp];
}
for (int i = K; i <= list.size(); i += K)
reverse(list.begin() + i - K, list.begin() + i);
for (int i = ; i < list.size(); ++i)
{
printf("%05d %d ", list[i], nodes[list[i]]);
if (i < list.size() - )
printf("%05d\n", list[i+]);
else
printf("-1\n");
}
return ;
}
最新文章
- Spring 02多种注入方式和注解实现DI
- [Java 基础]sun.misc.Unsafe
- Windows内核 基本汇编指令
- [ucgui] 对话框2——小窗口初始化与消息响应
- hdu 5437 Alisha’s Party 模拟 优先队列
- Tmall Programmer Triples Smartisan Sales
- 【转】FAE及其发展前景
- nagios总结二
- 浅谈MySQL分表
- 2.5. Integer 16 、Integer 32、Integer 64(Core Data 应用程序实践指南)
- 教你搭建你自己的Git服务器
- 用IDEA/WebStrom 提交本地项目到Git/码云等
- 其他-pkuwc2019数学考试题目
- 解决composer出错的原因
- 前端 使用 crypto-js 对数据进行对称加密
- WebRTC 学习之 Intel&#174; Collaboration Suite for WebRTC 关键类整理
- Reveal:分析iOS UI的利器
- 随手记:tomcat 与JDK 安装与配置
- HDU 2073 叠框
- OAuth 白话简明教程 4.刷新 Access Token
热门文章
- 【python】遇到的错误
- element-ui表格合计不显示&;被遮挡问题
- 2019-5-21-dotnet-使用-GC.GetAllocatedBytesForCurrentThread-获取当前线程分配过的内存大小...
- 当对象转换成JSON的时候处理时间格式
- Unable to resolve dependency for &#39;:app@debug/compileClasspath&#39;: Could not resolve com.android.support:appcompat-v7:26.1.0
- 本地git安装完成之后,从远程git服务器上面下载代码。报错SSL certificate problem:self signed certificate in certificate chain。
- Java 多线程 - 锁的类型
- 关于如何正确打开.wlf文件
- VS2010-MFC(菜单:VS2010菜单资源详解)
- vue-grid-layout