pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25)
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 <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
struct node{
int now,next,data;
};
node mem[];
vector<node> v;
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int h,now,data,next,num,k,temp;
scanf("%d %d %d",&h,&num,&k);
int i;
for(i=;i<num;i++){
scanf("%d %d %d",&now,&data,&next);
mem[now].now=now;
mem[now].data=data;
mem[now].next=next;
}
now=h;
while(now!=-){
v.push_back(mem[now]);
now=mem[now].next;
}
int length=v.size();
int round=length/k;
for(i=;i<round;i++){
reverse(v.begin()+i*k,v.begin()+i*k+k);
}
for(i=;i<length-;i++){
printf("%05d %d %05d\n",v[i].now,v[i].data,v[i+].now);
}
printf("%05d %d %d\n",v[i].now,v[i].data,-);//注意全反的情况
return ;
}
pat02-线性结构1. Reversing Linked List (25)的更多相关文章
- 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 ...
- 数据结构练习 02-线性结构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 ...
- 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 ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
- 02-线性结构2 Reversing Linked List
由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...
- 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 ...
随机推荐
- WinForm 中使用 Action 子线程对主线程 控制进行访问
/// <summary> /// 开启新线程执行 /// </summary> /// <param name="sender"></p ...
- VS Code基本使用
1. Activity Bar 1.1 Explorer 1.1.1. OPEN EDITORS 所有在右侧编辑区打开的文件列表 1.1.2 {PROJECTNAME} 某个文件夹下的文件树 1.2 ...
- ajaxfileupload 跨域 (二级域名) 可行办法
ajaxfileupload 跨二级域名 如 aa.fei.com 到 bb.fei.com 是可行的. 1.首先在html页面 ajaxfileupload上方加入 document.domain= ...
- 为什么要使用MQ消息中间件?
在面试大型互联网公司的时候,很可能会被问到消息队列的问题: 1.在何种场景下使用了消息中间件? 2.为什么要在系统里引入消息中间件? 3.如何实现幂等? 链式调用是我们在写程序时候的一般流程,为了完成 ...
- slowhttptest安装及使用
slowhttptest简介: Slowhttptest是依赖HTTP协议的慢速攻击DoS攻击工具,设计的基本原理是服务器在请求完全接收后才会进行处理,如果客户端的发送速度缓慢或者发送不完整,服务端为 ...
- HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube
就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...
- 1. C/C++笔试面试经典题目一
1. 不用循环和递归,实现打印数字0到999. #include <iostream> #include<stdio.h> using namespace std; #defi ...
- 导出table为Excel
1.HTML <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...
- 对KMP算法通过代码生成next数组理解
本文是根据考研数据结构2019版天勤高分笔记理解编写的: 首先给出代码: 1 void getnext(Str substr,int next[]){ 2 int i=0,j=0; 3 next[1] ...
- Qt 学习之路 2(58):编辑数据库外键
Qt 学习之路 2(58):编辑数据库外键(skip) 豆子 2013年7月12日 Qt 学习之路 2 13条评论 前面几章我们介绍了如何对数据库进行操作以及如何使用图形界面展示数据库数据.本章我们将 ...