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 ...
随机推荐
- JAVA-基础(Stream流)
说起stream流大家的第一反应是io,但是实际上谁规定流一定是存在io包里呢?在java8中得益于Lambda表达式的函数式编程,引入了一个全新的概念,stream. 1.优势? 在java8之前我 ...
- JS函数 编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数。
编程练习 使用javascript代码写出一个函数:实现传入两个整数后弹出较大的整数. 任务 第一步: 编写代码完成一个函数的定义吧. 第二步: 我们来补充函数体中的控制语句,完成函数功能吧. 提示: ...
- 和Excel函数date同样功能的VBA函数DateSerial用法
Sub 日期别()On Error Resume Nextlastrow = Sheets("运营日报").Range("a1048576").End(xlUp ...
- 威布尔weibull distribution
data = wblrnd(0.5,0.8,100,1); 生成威布尔随机函数,尺寸参数为0.5,形状参数为0.8,生成数列100行,一列: parmhat = wblfit(data) 对data的 ...
- Delphi 第一课
Delphi 是面向对象的 可视化的集成开发环境 对象包括属性 方法 事件. 用户事件 对象响应事件 不同的对象响应事件不一样
- adb命令 logcat日志抓取
一.logcat抓log方法:adb logcat命令,可以加条件过滤 1.安装SDK(参考android sdk环境安装) 2.使用数据线链接手机,在手机助手的sdcard中建立一个1.log的文件 ...
- 6.RDD算子实战
from pyspark import SparkContext,SparkConf import sys if __name__ == '__main__': if len(sys.argv) != ...
- C++如何判断某一文件是否存在
函数名: access 功 能: 确定文件的访问权限 用 法: int access(const char *filename, int amode); 程序例: #include <stdio ...
- 正则表达式 判断内容是否为合法的url
var strUrl= "^((https|http|ftp|rtsp|mms)?://)" + "?(([0-9a-z_!~*'().&=+$%-]+: )?[ ...
- Spring基础面试题(一)
Spring是什么? Spring是一个轻量级的IoC和AOP容器框架.是为Java应用程序提供基础性服务的一套框架,目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求.常见的配置方式有 ...