2019.12.7PAT冬季考试第二题 - 7-2 Block Reversing (25分)

【PTA刷题】链表题套路 PTA的链表其实和力扣的挺不一样的,PTA的其实都是用数组来模拟的,而且往往并不限制新开一个数组。出题模式比较固定。因此固定套路是把链表问题变成数组问题来做: 1.开一个装有结构体Node的固定数组,脚标为地址,模拟指针用。 2.从start给的地址开始遍历链表,把链表按顺序放入一个vector中。 3.根据给的条件来变换数组。将结果保存在ans数组中,最后打印出来。 1165.19冬季第二题 大意:Given a singly linked list L. Let us consider every K 阅读详情

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 (≤10​5​​) 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

作者: 陈越

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

这是一道块链表反转的题,也是自己比较有思路的一道题。

1.自己的解法

数组block_begin[]和block_end[],记录每个块的起点和终点的位置,然后将起点和终点反转即可,链表内部不需要动。

// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//


#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;

const int maxn = 100010;
int block_begin[maxn];
int block_end[maxn];
struct Node{
	int data, next;
}node[maxn];
/*
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
*/

int main(){
	int root_addr, n, k;
	cin>>root_addr>>n>>k;
	for(int i =0; i < n; i++){
		int addr, data, next;
		scanf("%d%d%d",&addr,&data,&next);
		node[addr].data = data;
		node[addr].next = next;
	}
	int block = 0; // block 1~n
	int final_num;
	if( n % k == 0){
		block = n / k;
		final_num = k;
	}
	else{
		block = n / k + 1;
		final_num = n % k;
	}
	int cur_ptr = root_addr;
	int cnt = 0;
	int block_idx = 1; // from 1 to n
	while(cur_ptr != -1){
		cnt++;
		if(cnt % k == 1) block_begin[block_idx] = cur_ptr;
		if(cnt % k == 0 || cnt == n) block_end[block_idx++] = cur_ptr;
		cur_ptr = node[cur_ptr].next;
	}
	root_addr = block_begin[block_idx - 1];

	for(int i = block_idx - 1; i >= 1; i--){
		if(i == 1)
		{node[block_end[i]].next = -1;}
		else
			node[block_end[i]].next = block_begin[i - 1];
	}
	
	cur_ptr = root_addr;
	while(cur_ptr != -1){
		if(node[cur_ptr].next != -1)
			printf("%05d %d %05d\n",cur_ptr, node[cur_ptr].data, node[cur_ptr].next);
		else
			printf("%05d %d %d\n",cur_ptr, node[cur_ptr].data, node[cur_ptr].next);
		cur_ptr = node[cur_ptr].next;
	}
	return 0;
}

 2大神的解法

使用sort的方法巧妙将链表反转:三阶排序:1.is0k 2.group 3.index

对于这道题来说,主要是group比较重要。

太巧妙了!在Node的结构里直接加入{group、index},初始化后进行排序!

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
	int data,address,next;
	int isOk;
	int group, index;
	Node (){
		isOk=0;
	}
}node[100010];
bool cmp(Node a, Node b){
	if (a.isOk!=b.isOk) return a.isOk>b.isOk;
	else if (a.group!=b.group) return a.group>b.group;
	else return a.index<b.index;
}
int main(){
	int n,k;
	int begin;
	scanf("%d %d %d",&begin, &n, &k);
	for (int i=0; i<n; i++){
		int address, data, next;
		scanf("%d %d %d",&address, &data, &next);
		node[address].data=data;
		node[address].address=address;
		node[address].next=next;
	}
	int p=begin;
	int cnt=0;
	//group a[group][index]
	while (p!=-1){
		node[p].group=cnt/k;
		node[p].index=cnt%k;
		cnt++;
		node[p].isOk=1;
		p=node[p].next;
	}
	sort(node, node+100010, cmp);
	for (int i=0; i<cnt; i++){
		if (i!=cnt-1){
			printf("%05d %d %05d\n",node[i].address, node[i].data, node[i+1].address);
		}else {
			printf("%05d %d -1\n",node[i].address, node[i].data);
		}
	}
	return 0;
}

 

3总结

链表题可以巧妙的使用sort的多级排序功能,出其不意!

19年冬季第二题 PAT甲级 1166 Summit (25分) 与题库1142类似的一般难度题 这道题没有什么坑,题意如下: 不超过200个人,编号从1到N 先给出朋友关系 再给出聚会人员编号问是不是全是朋友? 不是的话就求助;是的话,再分成两种 一种是没有一个朋友是没来聚会的 另一种是还能再请来聚会,就请编号小的 #include<iostream> #include<vector> using namespace std; int n,m,k; int main(... 阅读详情

相关推荐

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

qiiingc的解题记录 1537

2019年12月PAT甲级 第二题 Block Reversing(1165)题解

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

时间的玫瑰的博客 589

滤波器初步入门(非常全面)

主要关注的是-3dB这个点,-3dB就是截止频率所对应的点,我们把功率降至50%以下的信号定义为被衰减掉了(但要跟实际情况相结合,有些信号衰减-3db不够,需要衰减更多)-3dB这个点对应我们的电压来说是0.707。设计电路可以留一定的余量,如果你把4k设计为截止频率,那么4k时候衰减倍数为-3dB通常是不够的,你可以将截止频率设为2k,那么4k的衰减就可以达到15dB(假设),根据自己的需要去设计对应的截止频率。理想的滤波器是当输入的信号频率大于截止频率时,大于部分的频率对应的电压信号直接归0。

yybyybds的博客 5720

1165 Block Reversing

Given a singly linked listL. Let us consider everyKnodes as ablock(if there are less thanKnodes 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 inL. For example, givenLas 1→2→3

weixin_53199925的博客 468

PICO SCOPE 二次开发

基于C#开发一个简单的PICO示波器的上位机工作,与MCC600运动控制卡一起,实现数据自动采集

nixoncheng的博客 1149

卷积神经网络-猫狗识别(附源码)

利用python实现一个卷积神经网络,训练猫狗数据集,实现对猫狗图片的预测识别

m0_63223213的博客 2万+

PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)

7-2Block Reversing(25分) Given a singly linked listL. Let us consider everyKnodes as ablock(if there are less thanKnodes at the end of the list, the rest of the nodes are still con...

蔡军帅 810

用Python+Charles搞定小程序付费短剧下载(附完整代码与避坑点)

本文详细介绍了如何利用Python和Charles抓包工具实现小程序付费短剧的自动化下载。通过分析小程序视频接口、破解请求参数,并编写Python脚本,读者可以轻松获取并保存短剧内容。文章还提供了完整的代码示例和常见问题的解决方案,特别适合对视频采集和抓包技术感兴趣的开发者。

weixin_28731231的博客 1192

7-2 Block Reversing (25分)

7-2Block Reversing(25分) Given a singly linked listL. Let us consider everyKnodes as ablock(if there are less thanKnodes at the end of the list, the rest of the nodes are still considered as ...

~ 901

PCL 最小生成树用于法向量定向【2025最新版】

PCL 最小代价生成树用于点云法向量的定向。博客长期更新,本文最近更新时间为:2025年9月15日。

点云侠的博客 2068

Captcha must be filled out,解决Kaggle新用户注册无法弹出验证提示的问题

Captcha must be filled out

weixin_46713695的博客 5万+

PAT(甲级)2019年冬季考试

PAT(甲级)2019年冬季考试 自我测试,两个小时多一点做完,感觉比较简单吧,第一道题一开始审错了,浪费好多时间,先做后面三道,一个小时多一点做完,然后又花了一个小时做第一道题。。。。 7-1 Good in C When your interviewer asks you to write “Hello World” using C, can you do as the following figure shows? Input Specification: Each input file conta

qq_42877165的博客 354

基于STM32L4XX、HAL库的RTL8201F 以太网收发器(PHY)驱动程序设计

define PHY_RESET_DELAY 1000 // PHY复位延迟(ms)#define PHY_AUTONEGO_DELAY 3000 // 自动协商延迟(ms)/* 检查PHY ID (RTL8201F的ID为0x001CC816) *//* 系统时钟配置,确保ETH有50MHz时钟 *//* 初始化CPU, AHB和APB总线时钟 */

zhusl6688的博客 814

PAT-2019年冬季考试-甲级-7-2 Block Reversing (25分)

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

Eucalypt的博客 477

模型预测控制(MPC)解析(六):有约束的MPC简介

本文为阅读笔记,仅供学习交流使用!!! 先从一个例子开始,说明当原始设计的控制信号满足操作约束时,控制系统的性能会显著恶化。并且只要稍加修改,如果在控制器的实现中加入约束,则性能恶化的程度可以降低,从而产生约束控制的思想。 在学习有约束控制之前,先看一个控制系统在控制信号饱和和不饱和的情况下运行的例子。 例:无阻尼振荡器的数学模型如下: 采样周期为Δt=0.1\Delta{t}=0.1Δt=0.1,对应的离散时间状态空间模型为: 设计目标是:设计一个预测控制系统,使得系统输出跟踪单位阶跃响应越快越好。预

行者无疆的博客 6655

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

PAT(甲级)2019年冬季考试 7-2 Block Reversing (25分)

7-2 Block Reversing (25分) 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...

faith_Dy的博客 393

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PAT(甲级)2019年冬季考试 7-2 Block Reversing (25分)——模拟链表

7-2 Block Reversing (25分) 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

0 310

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

PAT(甲级)2019年冬季考试7-2 Block Reversing (25 分)

7-2 Block Reversing (25 分) 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. Fo

shshbdhsjjd的博客 216

PAT(甲级)2019年冬季考试 7-2 Block Reversing

这题是做过的,B1025,我还总结过,果然早晚复相逢,只改了一点点,见1025 反转链表。 点睛之笔是结构体数组的哈希,地址既做下标,又有实际含义,妙啊。 node[add].add = add; 当时应该是排序函数cmp写得很溜,今天下午模拟时就在那打算全靠hash,代码十分粗暴,得了20/30,也没兴趣再看错哪了。 加一个no属性用于排序是很好的。 此题的输出有点绕,主要是 1 要考虑最后一块可能不满K个。 2 要考虑-1作为唯一一个不是5位的地址会在何出出现。 总体思路没错的话,剩下

行步至春深 196

PAT(甲级)2019年冬季考试 7-1 Good in C (20分) 7-2 Block Reversing (25分) 7-3 Summit (25分) 7-4 Cartesian Tree (

7-1Good in C(20分) 读题比较麻烦,实际不难,注意输出格式就可以了。 #include<cstdio> #include<vector> #include<iostream> #include<queue> #include<cmath> #include<string> #include<alg...

Ike_Lin的博客 398

7-2 Block Reversing (25分) 【PAT 甲级19年冬考】

Given a singly linked listL. Let us consider everyKnodes as ablock(if there are less thanKnodes 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 inL. For example, givenLas 1→2→3...

ikunsaikou的博客 611

PAT(甲级)2019年冬季考试——题解+总结

PAT(甲级)2019年冬季考试 总结写在前头: 考了字符串、链表(伪链表)、图、树。考点比较均衡。 本次考试难度还行,需要较扎实的数据结构底子,对于字符串处理的技巧有一定的要求。 说句题外话,字符串是个重点,主要因为很多人会忽视字符串。2018-2019年的GPLT(团体程序设计天梯赛)的l1最后一题就是一道字符串处理,当时卡住了不知道多少人,但其实稍微做做字符串的题目之后就会发现,那题不过如此。这次PAT甲级的第一题也是不过如此,但很可惜,我当时也忽视了字符串的处理,错失甲满。。。 Good in

一个小渣渣的博客:) 1203

PAT2019年冬7-2 Block Reversing (25分)

Given a singly linked listL. Let us consider everyKnodes as ablock(if there are less thanKnodes 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 inL. For example, givenLas 1→2→3...

zy的博客 190

Reversing Linked List(7个测试点只过了3个,好难,放弃了)

#include <stdio.h> #include <stdlib.h> typedef struct Node *List; struct Node { int Address; int Data; int NxtAdd; List Next; }; List Create(int *N, int *K); void Print(List list); L...

qq_24041595的博客 583

2019年9月PAT甲级题目

。

l503301397的博客 413

pat甲级真题分类--模拟链表

7-2 Block Reversing (25 分) 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. Fo

wsfhdhjs的博客 189
上一篇: 2019.12.7PAT冬季考试第一题 - 7-1 Good in C (20分)
下一篇: 2019.12.7PAT冬季考试第二题 - 7-3 Summit (25分)
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值