19年冬季第二题 PAT甲级 1165 Block Reversing (25分) 跟1133类似的题目
题目
Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.
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 (≤10e5) which is the total number of nodes, and a positive K (≤N) which is the size of a block. 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 8 3
71120 7 88666
00000 4 99999
00100 1 12309
68237 6 71120
33218 3 00000
99999 5 68237
88666 8 -1
12309 2 33218
Sample Output:
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1
方法
这道题跟题目1133采用了部分相同的手法,就是先把节点读进a,再按照链表顺序读进v,最后再生成目标链表的顺序ans。
特别之处
从v到ans的赋值有不同的处理手法。这部分有时间可以思考一题多解:
https://blog.csdn.net/lm18600967236/article/details/103441535
更优化的方法
大家可以直接过去看,
2020年7月24日下午3点43分我看到的是作者那版代码有个小问题过不了最后一个测试点,
我已经在评论里指出问题所在。
他这个方法是更技巧化的但是更简单的一个方法。
https://blog.csdn.net/ikunsaikou/article/details/107507731
代码
#include<iostream>
#include<vector>
struct node{int id,data,next;
}a[1000005];
int main(){int begin,n,k,s,d,e;cin>>begin>>n>>k;vector<node> v,ans;for(int i=0;i<n;i++){scanf("%d %d %d\n",&s,&d,&e);a[s]={s,d,e};}for(;begin!=-1;begin=a[begin].next){v.push_back(a[begin]);}int len=v.size(),j=0;int m;int remain=len%k;ans.resize(len);for(int i=0;i<len;i++){if(i<len-remain){m=len/k-i/k-1;ans[k*m+j+remain]=v[i];j=(j+1)%k;}else{ans[i%k]=v[i];}}for(int i=0;i<len;i++){if(i!=len-1){printf("%05d %d %05d\n",ans[i].id,ans[i].data,ans[i+1].id);}else{printf("%05d %d -1",ans[i].id,ans[i].data);}}return 0;
}
19年冬季第二题 PAT甲级 1165 Block Reversing (25分) 跟1133类似的题目相关推荐
- 19年春季第二题 PAT甲级 1157 Anniversary(25 分)
英文题目 Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the cel ...
- 19年冬季第二题 PAT甲级 1166 Summit (25分)
7-3 Summit (25分) A summit (峰会) is a meeting of heads of state or government. Arranging the rest area ...
- 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼
题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...
- 【PAT - 甲级1003】Emergency (25分)(Dijkstra,最短路条数,双权值最短路)
题干: As an emergency rescue team leader of a city, you are given a special map of your country. The m ...
- 19年秋季第一题 PAT甲级 1161 Forever (20 分) 有点儿意思
如果喜欢我的文章请点赞让我知道噢 题目 7-1 Forever (20 分) "Forever number" is a positive integer A with K dig ...
- 【PAT - 甲级1010】Radix (25分)(二分,进制转化)
题干: Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? Th ...
- 【PAT甲级A1003 】Emergency (25分)(c++)
1003 Emergency (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB As an emergency rescue ...
- PAT甲级1139 First Contact (30 分):[C++题解] 图论、暴力枚举两个点、hash映射
文章目录 题目分析 题目链接 题目分析 来源:acwing 题目分析: 图论模拟题. 给定暗恋的两个人A 和B,需要寻找一对C 和D ,满足:A和C是朋友,C和D是朋友,D和B是朋友.而且A.C同性别 ...
- PAT甲级1131 Subway Map (30分):[C++题解]堆优化dijkstra、单源最短路、地铁地图、巧妙地建图套dijkstra模板!!
文章目录 题目分析 题目链接 题目分析 原题: 来源:acwing 分析: 建图:所有能走到的点之间建立一条边,比如下面一条地铁线路有4站,它们是相通的,两两之间建一条边,边权是经过的站点数. 下面考 ...
最新文章
- go系列(5)- beego自己写controller
- 几个极品笑话,放松下心情
- 求 1 到 n 的所有数的约数和
- 将军令:数据安全平台建设实践
- linux常用命令之权限
- 并查集判断是否有环存在
- Webpack:代码分割
- web developer tips (38):如何用请求失败记录追踪重写规则
- [论文阅读] Deep Automatic Natural Image Matting
- 使用Ext.grid.Panel生成表格
- HTML - 文本及其格式化
- 计算机java毕设_javaweb计算机毕设怎么做比较容易?
- 干货 | 基于JMP的方差分析及两两比较的思路与实现
- r语言 rgl 强制过程中_R语言中%||%是什么意思?
- python支持向量机 股票_测 python 利用SVM预测股票涨跌
- java 生成pdf文件_Java 生成PDF文档的示例代码
- win7电脑怎么提升开机速度
- threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
- 子平真诠释疑笔记(四)
- CSS —— 单行和多行文本实现省略号