当前位置:网站首页>区块反转(暑假每日一题 7)
区块反转(暑假每日一题 7)
2022-07-28 11:48:00 【sweetheart7-7】
给定一个单链表 L L L,我们将每 K K K 个结点看成一个区块(链表最后若不足 K K K 个结点,也看成一个区块),请编写程序将 L 中所有区块的链接反转。
例如:给定 L L L 为 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 1→2→3→4→5→6→7→8 1→2→3→4→5→6→7→8, K K K 为 3 3 3,则输出应该为 7 → 8 → 4 → 5 → 6 → 1 → 2 → 3 7→8→4→5→6→1→2→3 7→8→4→5→6→1→2→3。
补充
本题中可能包含不在单链表中的节点,这些节点无需考虑。
输入格式
第 1 1 1 行给出第 1 1 1 个结点的地址、结点总个数正整数 N N N、以及正整数 K K K,即区块的大小。结点的地址是 5 5 5 位非负整数(可能包含前导 0 0 0),NULL 地址用 −1 表示。
接下来有 N N N 行,每行格式为:
Address Data Next
其中 Address 是结点地址,Data 是该结点保存的整数数据,Next 是下一结点的地址。
输出格式
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
数据范围
1 ≤ K ≤ N ≤ 1 0 5 1≤K≤N≤10^5 1≤K≤N≤105
输入样例:
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
输出样例:
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1
#include<iostream>
#include<vector>
using namespace std;
const int N = 100010;
int n, k;
int e[N], ne[N];
int main(){
int h = -1;
scanf("%d%d%d", &h, &n, &k);
int addr, data, neaddr;
for(int i = 0; i < n; i++){
scanf("%d%d%d", &addr, &data, &neaddr);
e[addr] = data;
ne[addr] = neaddr;
}
vector<int> v, res;
for(int i = h; i != -1; i = ne[i])
v.push_back(i);
int t = v.size() / k;
if(v.size() % k != 0) t++;
for(int i = t - 1; i >= 0; i--)
for(int j = i * k; j < (i+1) * k && j < v.size(); j++)
res.push_back(v[j]);
for(int i = 0; i < res.size(); i++){
printf("%05d %d ", res[i], e[res[i]]);
if(i == res.size() - 1) puts("-1");
else printf("%05d\n", res[i + 1]);
}
return 0;
}
边栏推荐
- Analysys analysis: focus on users, improve the user experience of mobile banking, and help the growth of user value
- AVL tree (balanced search tree)
- Did kafaka lose the message
- Developing NES games with C language (cc65) 08. Background collision
- Let Arduino support nuvotom Xintang
- 恋爱男女十禁
- VS1003调试例程
- 【萌新解题】爬楼梯
- 【vulnhub】presidential1
- Implementation method of mouse hover, click and double click in ue4/5
猜你喜欢

VS1003调试例程

If you don't roll the golden nine and silver ten, it's too late

HC-05蓝牙模块调试从模式和主模式经历

VS code更新后不在原来位置

03 pyechars 直角坐标系图表(示例代码+效果图)

行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开

Distributed session solution

非标自动化设备企业如何借助ERP系统,做好产品质量管理?
![[cute new problem solving] climb stairs](/img/a2/fd2f21c446562ba9f0359988d97efe.png)
[cute new problem solving] climb stairs

AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!
随机推荐
Siemens docking Leuze BPS_ 304i notes
[apue] 文件中的空洞
用C语言开发NES游戏(CC65)04、完整的背景
第九章 REST 服务安全
开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
Distributed session solution
XIII Actual combat - the role of common dependence
Exploration on cache design optimization of community like business
On Governance and innovation | the openanolis sub forum of the 2022 open atom global open source summit was successfully held
用arduino开发ESP8266 搭建开发环境
FlexPro软件:生产、研究和开发中的测量数据分析
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
C for循环内定义int i变量出现的重定义问题
GMT安装与使用
Developing NES games with C language (cc65) 06. Sprites
HC-05蓝牙模块调试从模式和主模式经历
[Nuxt 3] (十二) 项目目录结构 3
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
西门子对接Leuze BPS_304i 笔记