PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
来源:互联网 发布:避风港淘宝客 编辑:程序博客网 时间:2024/06/11 11:24
1025. 反转链表 (25)
时间限制
300 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入格式:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出格式:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入样例:00100 6 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218输出样例:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
#include <cstdio>#include <algorithm>using namespace std;const int maxn=1e5+10;struct Node{ int address,data,next; int order; bool operator < (const Node& A)const{ return order<A.order; }}node[maxn];int main(){ for(int i=0;i<maxn;i++) node[i].order=maxn; int begin,n,k,address; scanf("%d%d%d",&begin,&n,&k); for(int i=0;i<n;i++){ scanf("%d",&address); scanf("%d%d",&node[address].data,&node[address].next); node[address].address=address; } int p=begin,cnt=0; while(p!=-1){ node[p].order=cnt++; p=node[p].next; } sort(node,node+maxn); n=cnt; for(int i=0;i<n/k;i++){ for(int j=(i+1)*k-1;j>i*k;j--){ printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address); } printf("%05d %d ",node[i*k].address,node[i*k].data); if(i<n/k-1) printf("%05d\n",node[(i+2)*k-1].address); else{ if(n%k==0) printf("-1\n"); else{ printf("%05d\n",node[(i+1)*k].address); for(int i=n/k*k;i<n;i++){ printf("%05d %d ",node[i].address,node[i].data); if(i<n-1) printf("%05d\n",node[i+1].address); else printf("-1\n"); } } } } return 0;}
阅读全文
0 0
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)C语言
- PAT (Basic Level) Practise (中文) 1025. 反转链表 (25)
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- PAT Basic Level 1025. 反转链表(25)
- PAT (Basic Level) Practise (中文)1075. 链表元素分类(25)
- PAT (Basic Level) Practise (中文) 1075. 链表元素分类(25)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)--1001
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)1001
- PAT (Basic Level) Practise (中文)1002
- PAT (Basic Level) Practise (中文) 1007
- PAT (Basic Level) Practise (中文)1015. 德才论 (25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT (Basic Level) Practise (中文)1020. 月饼 (25)
- python的正则表达式
- jdk1.8新特性
- 【资源共享】CC2541之新手常见问题集
- 异常处理 判断三角形三条边是否构成三角形
- Netty 权威指南笔记(六):Channel 解读
- PAT (Basic Level) Practise (中文)1025. 反转链表 (25)
- input回车事件未实现但页面刷新
- 亲授MVC5中EF6 Code First启动慢及间隙变慢优化的实践经验
- Java面向对象基础知识点
- ABP官方文档(四十九)【集成EntityFramework】
- 请定义一个交通工具(Vehicle)的类,其中有: 属性:速度(speed),体积(size)等 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(s
- 关于调节学习率(learning rate)的几点建议
- Android studio安装
- 服务器上的 Git