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 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
#include <stdio.h>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
using namespace std;
const int maxn = ;
struct node{
int addr;
int data;
int next;
}nodes[maxn];
vector<node> v;
int main(){
int st, n, k, count = ;
cin >> st >> n >> k;
for (int i = ; i < n; i++){
int start, data, next;
cin >> start >> data >> next;
nodes[start].addr = start;
nodes[start].data = data;
nodes[start].next = next;
}
while (st != -){
v.push_back(nodes[st]);
st = nodes[st].next;
count++;
}
for (int i = ; i+k <= count; i = i + k){
reverse(v.begin() + i, v.begin() + i + k);
}
for (int i = ; i < count-; i++){
v[i].next = v[i + ].addr;
}
v[count-].next = -;
for (int i = ;i < count-; i++){
printf("%05d %d %05d\n", v[i].addr, v[i].data, v[i].next); }
printf("%05d %d %d\n", v[count - ].addr, v[count - ].data, v[count - ].next);
system("pause");
}
注意点:同B1025,链表题,都会要你重新排序输出,注意可以用vector实现链表,不要用静态数组,保存节点时一定要保存自己的地址。反转可以直接使用algorithm里的reverse
PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse的更多相关文章
- 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 ...
- 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 ...
- 【PAT甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 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 ...
- 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 ...
- 浙大数据结构课后习题 练习二 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 ...
- PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)
7-2 Block Reversing (25分) Given a singly linked list L. Let us consider every K nodes as a block ( ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
随机推荐
- maven+eclipse创建web项目
第一步,创建maven工程,如下图步骤 选择maven-archetype-webapp,然后next 输入GroupId和ArtifactId,Package可以为空,然后finish 新创建的ma ...
- Error running tomcatUnable to open debugger port (127.0.0.1 50181) java.net.B
1.把tomcat删除,重新建一个,tomcat就会重新自动使用一个端口了 2.自己手动调一下端口 我用的是第二种,手动调了下,最开始的端口是:50181,该成了50182,就可以了.
- 2018-02-17 中文代码示例[译]Scala中创建隐式函数
前言: 学习Scala时, 顺便翻译一下自己有兴趣的文章. 代码中所有命名都中文化了(不是翻译). 比如原文用的是甜甜圈的例子. 原文: Scala Tutorial - Learn How To C ...
- 2018-02-06 编程猫IDE体验:对Scratch的改进
前两天偶遇编程猫推介(为什么没有中文的编程?), 第一眼感觉像Scratch, 求证之下确实, 并且据说有改良. 今天非常粗浅地尝试一下, 限于水平没有做出很炫的效果, 不过颇有些发现. 首先上最终效 ...
- 【读书笔记】iOS-发布你的应用
一,添加图标 你的应用在iPhone主屏幕上的标准图标(Icon.png)是57像素*57像素的正方形,PNG格式,不能有透明效果或者图层,72DPI.除些之外,你还可以提供一个同样格式的114像素* ...
- 新浪微博POI点签到数据及可视化的初步成果
目前仅对山东省区域进行了抓取,权限不够高,抓取的速度非常慢,所以导致效率比较低... 数据抓取采用调用微博开放平台API的方法,数据存储采用mysql,格点数据分辨率为30″,山东省的MBR范围内(包 ...
- Kotlin入门(12)类的概貌与构造
上一篇文章提到泛型函数appendString是在类外面定义,这不免使人疑惑,类里面又该怎样定义成员函数呢?为解答这个疑问,接下来的几篇文章将好好描述一下Kotlin如何操作类及其对象,本篇文章先对类 ...
- VirtualBox下安装CentOS7系统
本文假定你已经知道如何安装VirtualBox虚拟机软件,并且已经安装好了. 首先我们需要准备好centos的iso镜像文件,可以从centos的官网下载. 以下操作使用的VirtualBox版本号是 ...
- Python中DataFrame去重
# 去除重复行数据 keep:'first':保留重复行的第一行,'last':保留重复行的最后一行,False:删除所有重复行df = df.drop_duplicates( subset=['YJ ...
- 使用Python语言理解递归
递归 一个函数在执行过程中一次或多次调用其本身便是递归,就像是俄罗斯套娃一样,一个娃娃里包含另一个娃娃. 递归其实是程序设计语言学习过程中很快就会接触到的东西,但有关递归的理解可能还会有一些遗漏,下面 ...