02-线性结构1. Reversing Linked List (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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)的更多相关文章

  1. 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 ...

  2. 数据结构练习 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 ...

  3. 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 ...

  4. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  5. 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 ...

  6. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. 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 ...

  8. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

  9. 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 ...

随机推荐

  1. SQL 2005报错之Restore fail for Server 'DatabaseServerName'.

    Restore fail for Server 'DatabaseServerName'.(Microsoft.SqlServer.Smo) Additional information: Syste ...

  2. C# 筛选string 类型里面的汉字,获取首字母字母,正则表达式Regex 常用验证

    界面效果 1.提取汉字 private void buttonX1_Click(object sender, EventArgs e) { if (TxtYuan.Text.Trim() != &qu ...

  3. ubuntu 14.04 x64下安装libreoffice

    LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...

  4. NPOI 2.1.3.1导入Excel

    引入NPOI 2.1.3.1的包 项目引入 using NPOI.XSSF.UserModel;using NPOI.SS.UserModel; 控制器方法: public ActionResult ...

  5. C# LINQ(3)

    我们还是接着讨论一下group by 这一章节讨论group的本质:分组. 分组之后进行存储或者查询. 这个时候就要用一个新的关键字:into 这个之后就group就不作为结尾了. 必须重写另起sel ...

  6. 第八篇 Python异常

    程序在运行时,如果Python解释器遇到一个错误,会停止程序的执行,并且提示一些错误信息,这就是异常,程序通知执行并且提示错误信息,这个动作,我们通常称之为:抛出异常. 1.简单的捕获异常的语法 在程 ...

  7. Eclipse内存不足 增加eclipse的运行内存

    自己解决的 三.    修改Run Configurations (此方法可行) 在代码上右键,依次点击“Run As ”-> “Run Configurations ”,在Arguments  ...

  8. django自定义rbac权限组件(二级菜单)

    一.目录结构 二.表结构设计 model.py from django.db import models # Create your models here. class Menu(models.Mo ...

  9. [Swift]八大排序算法(五):插入排序

    排序分为内部排序和外部排序. 内部排序:是指待排序列完全存放在内存中所进行的排序过程,适合不太大的元素序列. 外部排序:指的是大文件的排序,即待排序的记录存储在外存储器上,待排序的文件无法一次装入内存 ...

  10. Ubuntu系统使用apache部署多个django项目(python4.3)

    /etc/apache2/sites-available/pyweb.conf <VirtualHost *:> ServerName 192.168.1.46 DocumentRoot ...