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 ...
随机推荐
- 【转】Git 教程之协同开发
前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协同开发的基础,也是代码备份的 ...
- 【读书笔记】iOS-流式音频与Pandora Radio之路
复杂性是不可避免的,而且只会随时间增长,所以在增加特性时一定要为重构和代码简化留出时间.真正遇到问题这前先不要担心性能.iPhone非常强壮,你可能永远也不会遇到预想的性能问题. 能过互联网向一个设备 ...
- 【机器学习】激活函数(ReLU, Swish, Maxout)
https://blog.csdn.net/ChenVast/article/details/81382939 神经网络中使用激活函数来加入非线性因素,提高模型的表达能力. ReLU(Rectifie ...
- Python从入门到精通
最近研究了一下Python,名不虚传,确实挺精彩. 学习一门新的语言,我认为从入门到精通的做法是:下SDK.装IDE.练教程.结合工作应用.不断踩坑进阶.梳理总结 1.下SDK(2.7.15) 下载地 ...
- 使用sklearn机器学习库实现线性回归
import numpy as np # 导入科学技术框架import matplotlib.pyplot as plt # 导入画图工具from sklearn.linear_model imp ...
- Spring boot 入门篇
详见:https://www.cnblogs.com/ityouknow/p/5662753.html 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架, ...
- jmeter利用自身代理录制脚本
在利用代理录制脚本时一定要安装java jdk,不然不能录制的. 没有安装过java jdk安装jmeter后打开时会提示安装jdk,但是mac系统中直接打开提示安装jdk页面后下载的java并不是j ...
- python第一百零二天-----第十七周作业
由于内容众多 直接使用 git 链接 : https://github.com/uge3/hosts_masg 主机管理WEB页面 使用 SQLALchemy 主机管理(8列) ip 用户表: 用户名 ...
- VS发布web应用程序报:无法识别的特性“xmlns:xdt”。请注意特性名称区分大小写 或 未能将文件obj\...复制到obj\...未能找到路径
问题1:无法识别的特性“xmlns:xdt”.请注意特性名称区分大小写 问题2:未能将文件obj\...复制到obj\...未能找到路径 解决办法:将web项目文件下的obj文件夹从项目中排除,然后再 ...
- 理解inode 以及 软链接和硬链接概念区分
inode简单理解 本文来源自网络文章,并针对文章内容加以批注和修改.希望能帮到你! 一. 磁盘设备 说到inode,首先必须要提及下<操作系统>中磁盘存储器的管理一节.磁盘设备是一种相当 ...