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 ;
}
PAT甲级——A1074 Reversing Linked List的更多相关文章
- 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 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- A1074. Reversing Linked List
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 ...
- 【PAT甲级】1052 Linked List Sorting (25 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- PAT_A1074#Reversing Linked List
Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
随机推荐
- 解决通过vmware克隆虚拟机后,无法上网的问题
注意:如果源主机是CentOS 6.8,复制出来的机器会出现无法上网. 如果源主机是CentOS 7,复制出来的机器可以正常上网.复制后,只要改下IP地址即可上网. 出现该问题的原因是,我们克隆后,将 ...
- PROJECT | 四则运算UI设计 - PSP表格&需求分析
PSP表格(TP版) 需求分析 [GUI编程语言选择] 考虑到Java编写GUI效率偏低且界面不算特别美观(即使有Windowbuilder插件帮助),所以我们使用控件更多,开发效率更高,具有集成开发 ...
- PHP魔方解密
安装composer参考:https://www.runoob.com/w3cnote/composer-install-and-usage.html 常用的加密类型及特征 加密类型 加密特征 Zen ...
- C# 调用java的Webservice时关于非string类型处理
比如webservice地址是:http://wdft.com:80/services/getOrderService1.0?wsdl 方法是:getOrder 1.首先添加引用: 2. 3.引用完成 ...
- AOP与IOC的概念(即spring的核心)
a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而spring的核心是IOC控制反转和AOP面向切面 ...
- Font Awesome (Mark)
Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. 一个字库,675个图标 仅一个Font Awesome ...
- 时间 '2018-08-06T10:00:00.000Z' 格式转化为本地时间(转)
原文:https://blog.csdn.net/sxf_123456/article/details/81582964 from datetime import datetime,timedelta ...
- The Battle of Chibi
The Battle of Chibi 给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\). 解 不难想到设\ ...
- IDEA快捷键(收集自网络后整理)
快捷键 说明 CTRL+B 快速打开光标处的类或方法 CTRL+C 拷贝 CTRL+D 复制当前行到下一行 CTRL+E 最近打开的文件 CTRL+F 当前文件查找特定内容 CTRL+G 定位行 CT ...
- scala中Array简单实用
/** * 在scala中数组的使用 * 和java很类似,初始化后,长度就固定了,而且元素全部根据其类型初始化 * */ object arrayUse { def main(args: Array ...