【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 Lbeing 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
总结:
- 学习了柳神的代码,for循环里的真漂亮
代码:
#include <iostream>
using namespace std;
int main() {int first, k, n, sum = 0;cin >> first >> n >> k;int temp, data[100005], next[100005], list[100005], result[100005];for (int i = 0; i < n; i++) {cin >> temp;cin >> data[temp] >> next[temp];}while (first != -1){list[sum++] = first;first = next[first];}for (int i = 0; i < sum; i++) result[i] = list[i];for (int i = 0; i < (sum - sum % k); i++)result[i] = list[i / k * k + k - 1 - i % k];for (int i = 0; i < sum - 1; i++)printf("%05d %d %05d\n", result[i], data[result[i]], result[i + 1]);printf("%05d %d -1", result[sum - 1], data[result[sum - 1]]);return 0;
}
【PAT】A1074 Reversing Linked List ***相关推荐
- 【PAT】PAT总结《搜索、打表、分治、排序算法、队列、栈、堆、Hash》
打表 打表这个技巧其实我们在素数部分的题已经涉及到了.还是要仔细思考,什么时候应该打表,打表有助于减少后续的计算的时候,我们应该打表, 特别是查询量特别大,即时计算无论多么快都会超时的情况.或者逆向运 ...
- 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)
在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...
- PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector
文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...
- 【ZOJ3899】State Reversing 解题报告
[ZOJ3899]State Reversing Description 有\(N\)个不同的怪兽,编号从\(1\) 到\(N\).Yukari有\(M\)个相同的房间,编号为\(1\)到\(M\). ...
- 【TensorRT】TensorRT was linked against cudnn 8.6.0 but loaded cudnn 8.3.2
[TensorRT]TensorRT was linked against cudnn 8.6.0 but loaded cudnn 8.3.2 1 电脑环境 2 问题描述 3 直接原因 4 解决办法 ...
- 【PAT】乙级题目解答合集(c++)
[PAT]乙级题目解答合集(c++) 本篇文章为对PAT乙级1001-1095的题目解答的汇总 1001 害死人不偿命的(3n+1)猜想 (15 分) 1002 写出这个数 (20 分) 1003 我 ...
- 【PAT】第四章 算法初步
第四章 算法初步 目录 第四章 算法初步 4.1 排序 4.1.1 选择排序 4.1.2 插入排序 4.1.3 排序题与sort函数的应用 strcmp 计算排名 4.2 散列 4.2.1 散列 1. ...
- 【PAT】(B)1094 谷歌的招聘 (20 分)
『题意描述』 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com 结尾的网址,而前面的网址是一个 10 位素数,这个素数是 ...
- 【PAT】A1090 Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
最新文章
- 字节跳动《Python高频面试题》火了,完整版 PDF 开放下载!
- 聊聊flink Table的ScalarFunction
- 对“相亲节目整风”的看法
- 【discuz x3】源代码中的sql调用
- hj212协议数采仪_CEMS系统配套用环保数采仪 HJ212协议上传
- 数据库逻辑删除的sql语句_SQL查询优化的数据库设计和逻辑断言
- 20211010 PHP笔记
- 详解站长之家之站长工具四大新功能
- CAD导出.eps格式图
- IT服务器台账系统,it项目管理信息系统
- 使用Sugar制作数据可视化大屏的操作过程
- 沈阳航空航天大学计算机考研真题知识点摘要
- 为什么选择ROS2编程作为机器人工程课程之一?
- html / CSS 自定义字体font 自己设置好看的特效字体
- 使用PYthon绘制小狗狗来讨 girl friend 喜欢
- 圣人、君子、小人、庸人的划分标准
- Linux查看目录大小文件大小内存大小硬盘大小
- Unity shader build 打包android和PC机显示效果不一样
- Redis介绍及5种数据类型的使用
- matlab 计算变异系数,(案例)基础统计指标Matlab计算函数
热门文章
- 整理:关于聚簇索引和非聚簇索引的区别
- 2007年教育学专业基础综合考试大纲
- Day02-深度学习原理与使用方法
- 论文《Attention Is All You Need》及Transformer模型
- ECC椭圆曲线算法(4)签名验证过程
- 进程间通信(2) 内存映射FileMap
- C++ Primer 5th笔记(6)chapter6 函数: 重载
- 深度探索C++ 对象模型(7)-Data member的布局(多重继承)
- 数学建模——一维、二维插值模型详解Python代码
- 【数据库复习】第一章绪论