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 ;
}

最新文章

  1. Spring 02多种注入方式和注解实现DI
  2. [Java 基础]sun.misc.Unsafe
  3. Windows内核 基本汇编指令
  4. [ucgui] 对话框2——小窗口初始化与消息响应
  5. hdu 5437 Alisha’s Party 模拟 优先队列
  6. Tmall Programmer Triples Smartisan Sales
  7. 【转】FAE及其发展前景
  8. nagios总结二
  9. 浅谈MySQL分表
  10. 2.5. Integer 16 、Integer 32、Integer 64(Core Data 应用程序实践指南)
  11. 教你搭建你自己的Git服务器
  12. 用IDEA/WebStrom 提交本地项目到Git/码云等
  13. 其他-pkuwc2019数学考试题目
  14. 解决composer出错的原因
  15. 前端 使用 crypto-js 对数据进行对称加密
  16. WebRTC 学习之 Intel&#174; Collaboration Suite for WebRTC 关键类整理
  17. Reveal:分析iOS UI的利器
  18. 随手记:tomcat 与JDK 安装与配置
  19. HDU 2073 叠框
  20. OAuth 白话简明教程 4.刷新 Access Token

热门文章

  1. 【python】遇到的错误
  2. element-ui表格合计不显示&amp;被遮挡问题
  3. 2019-5-21-dotnet-使用-GC.GetAllocatedBytesForCurrentThread-获取当前线程分配过的内存大小...
  4. 当对象转换成JSON的时候处理时间格式
  5. Unable to resolve dependency for &#39;:app@debug/compileClasspath&#39;: Could not resolve com.android.support:appcompat-v7:26.1.0
  6. 本地git安装完成之后,从远程git服务器上面下载代码。报错SSL certificate problem:self signed certificate in certificate chain。
  7. Java 多线程 - 锁的类型
  8. 关于如何正确打开.wlf文件
  9. VS2010-MFC(菜单:VS2010菜单资源详解)
  10. vue-grid-layout