PAT甲级 A1098

PAT甲级题目解析和知识点分类整理 个人博客:https://maxusun.github.io/ 转载请注明出处 今天整理电脑,发现了去年为了考研浙大计算机整理的PAT资料。现在考研已经尘埃落定。想到当时盲目刷题浪费了好多时间,在这里希望将整理的东西放到网上,方便后来者学习。 PAT的命题趋势 将PAT甲级中的题目从第一题刷到最后一题,可以明显感受到,PAT甲级的题目风格发生了很明显的变化。从早期的模拟型题目变成了后来以数据结构和算法为主的固定题型。如果时间紧迫的话,可以只刷后一般的题目,放弃前一半的题目,因为前一半题目的出题风格已经过时了 阅读详情

PAT甲级 A1098

题目详情

1098 Insertion or Heap Sort (25分)
According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in the first line either “Insertion Sort” or “Heap Sort” to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Sample Input 2:
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9、

解题思路

这道题与前面一道题非常像,无非是用堆排序进行
堆排序的实现方法:一开始根据不同的子树建立大根堆,而后分别将堆顶堆低互换向下下沉,使得其变为从小到大的有序排序

以下为AC代码

#include <cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<map>
#include<queue>;
using namespace std;
int N[200]; int K;
int ans[200];
bool issame(int a[200]) {
	for (int i = 0; i < K; i++) {
		if (ans[i] != a[i]) {
			return false;
		}
	}
	return true;
}
bool issame2(int a[200]) {
	for (int i = 0; i < K; i++) {
		if (ans[i] != a[i+1]) {
			return false;
		}
	}
	return true;
}
bool insertsort() {
	int temp[200];
	copy(N, N + K, temp);
	for (int i = 1; i < K; i++) {
		for (int j = i - 1; j >= 0; j--) {
			if (temp[j] > temp[j + 1]) {
				swap(temp[j], temp[j + 1]);
			}
			else {
				break;
			}
		}
		if (issame(temp)) {
			cout << "Insertion Sort" << '\n';
			i = i + 1;
			for (int j = i - 1; j >= 0; j--) {
				if (temp[j] > temp[j + 1]) {
					swap(temp[j], temp[j + 1]);
				}
				else {
					break;
				}
			}
			for (int t = 0; t < K; t++) {
				if (t != 0) {
					cout << " " << temp[t];
				}
				else {
					cout << temp[t];
				}
			}
			cout << '\n';
			return true;
		}

	}
	return false;
}
bool heapsort() {//需要建立一个大根堆
	int temp[200];
	int p = 0;
	copy(N, N + K, temp+1);
	int begin = K / 2;
	for (int i = begin; i > 0; i--) {//建立初始堆
		int pos = i;
		while (pos <= K) {
			if (2 * pos > K) {
				break;
			}
			if (2 * pos == K) {
				if (temp[2 * pos] > temp[pos]) {
					swap(temp[2 * pos], temp[pos]);
					pos = 2 * pos;
					break;
				}
				else {
					break;
				}
			}
			else if ((2 * pos + 1) <= K) {
				int num = max(temp[2 * pos], temp[2 * pos + 1]);
				if (num <= temp[pos]) {
					break;
				}
				else {
					if (num == temp[2 * pos]) {
						swap(temp[2 * pos], temp[pos]);
						pos = 2 * pos;
					}
					else {
						swap(temp[2 * pos + 1], temp[pos]);
						pos = 2 * pos + 1;
					}
				}
			}
		}
	}//建堆结束
	for (int i = K ; i > 0; i--) {
		if (i < K  && issame2(temp)) {
			p = 1;
		}
		swap(temp[i], temp[1]);
		//更换后重新开始下沉
		int pos = 1;
		while (pos <= i-1) {
			if (2 * pos > i-1) {
				break;
			}
			if (2 * pos == i-1) {
				if (temp[2 * pos] > temp[pos]) {
					swap(temp[2 * pos], temp[pos]);
					pos = 2 * pos;
					break;
				}
				else {
					break;
				}
			}
			else if ((2 * pos + 1) <= i-1) {
				int num = max(temp[2 * pos], temp[2 * pos + 1]);
				if (num <= temp[pos]) {
					break;
				}
				else {
					if (num == temp[2 * pos]) {
						swap(temp[2 * pos], temp[pos]);
						pos = 2 * pos;
					}
					else {
						swap(temp[2 * pos + 1], temp[pos]);
						pos = 2 * pos + 1;
					}
				}
			}
		}
		if (p == 1) {
			cout << "Heap sort" << '\n';
			for (int t = 1; t <= K; t++) {
				if (t != 1) {
					cout << " " << temp[t];
				}
				else {
					cout << temp[t];
				}
			}
			cout << '\n';
			return true;
		}
	}
	return false;
}
int main() {
	cin >> K;
	for (int i = 0; i < K; i++) {
		scanf_s("%d", &N[i]);
	}
	for (int i = 0; i < K; i++) {
		scanf_s("%d", &ans[i]);
	}
	if (!insertsort()) {
		heapsort();
	}
}

Android 的端智能学习路径 基础层:先补齐 Android 进阶(JNI / 性能优化)和 AI 基础概念,无需深入算法开发;核心层:掌握 TFLite 的模型加载、推理、量化,能完成基础端侧推理;落地层:聚焦实战场景(图像 / OCR / 语音),解决工程化问题(模型管理、异常处理);优化层:通过模型量化、硬件加速、工程优化,提升推理性能和用户体验。 阅读详情

相关推荐

SAP 分期付款,付款条件的要件定义方法

以上内容做好后,可以在F-02或者F-60里看设置后的结果。找到相关路径选择【维护支付条件】点击进入后,选择【新条目】再设置一个总的支付规则,注意这里【付款条款】一定要选择。例:默认付款条件SP00,改成刚定义好的0028即可。将刚才基础创建的付款条件,以以下方式录入,注意。028A- 30天到期,过账日期基础。028B- 60天到期,过账日期基础。028C- 90天到期,过账日期基础。目的:新建支付条款(本次以。的选择,然后保存后,完成。,【基准日期缺省值】选择。基础创建好了以后,选择【

晴天的博客分享技术知识 2936

PAT——1098 Insertion or Heap Sort 甲级(超详细注释)

1098 Insertion or Heap Sort题目题意代码思路AC代码参考 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805368847187968 题意 首先,不了解堆排序流程的同学可以看看我的第二篇参考文章(插入排序较为简单,不会的同学自行学习) 题目给出两个序列a、b,a为最初序列,b为排序过的序列,问排序方式是插入排序还是堆排序,并输出下一步的排序结果 代码思路 代码思路详见参考文章第一篇 需要说明的是,

ljhsq的博客 492

Vivado时序违约小结

目录1. setup time违约1.1 逻辑级数太高1.2 Fanout太大2. hold time 违约2.1 异步信号处理导致hold time违约 1. setup time违约 建立时间是工程设计中最常遇到的问题了。一般说来,导致建立时间违例主要有两个原因:逻辑级数太大或者扇出太大,当然也可以尝试通过降频来解决建立时间违约。 1.1 逻辑级数太高 Levels指的是逻辑级数logic level,一个logic level的延迟对应的是一个LUT和一个Net的延迟,对于不同的器件,不同频率的设计能

mu_guang_的博客 7155

PAT A1098 堆排序

完整的堆排序内容; 其中给出了一个思路,就是对于插入排序,使用sort函数会快很多。。。这也算模拟经典排序的一种取巧方式 #include&lt;iostream&gt; #include&lt;stdlib.h&gt; #include&lt;stdio.h&gt; #include&lt;vector&gt; #include&lt;...

weixin_33978044的博客 216

不只是烧录:深入理解英飞凌Aurix MCU的HSM安全模块与UDE Memtool解锁操作

本文深入探讨了英飞凌Aurix MCU的HSM安全模块设计与UDE Memtool解锁操作,揭示了硬件安全模块的架构、安全启动链机制及调试接口的安全特性。通过分析UDE Memtool的解锁逻辑和量产环境下的最佳实践,帮助工程师更好地应对MCU锁定问题,提升汽车电子和工业控制领域的安全烧录效率。

weixin_32770423的博客 298

告别手动拍照!用Python+JAKA机械臂+D455相机实现全自动手眼标定(附完整代码)

本文详细介绍了如何利用Python脚本实现JAKA机械臂与Intel RealSense D455相机的全自动手眼标定流程,提供从硬件配置到代码实现的完整解决方案。通过自动化标定,显著提升工业自动化中的精度和效率,适用于机器人视觉领域的二次开发需求。

weixin_30555125的博客 521

[PAT] A1098 Insertion or Heap Sort

(重要!堆!) 题目大意 给出n和n个数的序列a和b,a为原始序列,b为排序其中的一个步骤,问b是a经过了堆排序还是插入排序的,并且输出它的下一步。 坑 测试点2:插入排序时,要考虑输入的两个序列是相同的情况。即第一次判断时原序列的第一个数和第二个数本身有序(题目保证了答案唯一,所以不会出现连续几个本身都有序的情况)。一开始我写错了,是因为先判断了序列是否相同再进入循环。 化简 找到两个...

yue的博客 188

音频链路分析

音频链路分析摘要 本文分析了蓝牙音频设备(BES2700)的音频传输链路,主要包括通话和音乐播放两种场景: 通话链路: 下行:蓝牙接收数据→解码→通过Codec输出到扬声器 上行:麦克风采集→Codec处理→蓝牙编码发送 音乐播放链路: 通过A2DP协议接收高质量音频数据 经过解码、EQ调节等音频处理后输出 系统采用多线程消息机制处理音频任务,通过mailbox传递控制消息,由回调函数处理具体音频数据流。不同应用场景使用不同的蓝牙协议栈(HFP/HSP用于通话,A2DP用于媒体)和编码方式(mSBC/SBC

云淡风轻 85

PAT乙级 1023 组个最小数

#include<iostream> #include<string> #include<map> #include<algorithm> #include<bits/stdc++.h> using namespace std; int main(void){ int n; vector<int> vec; for(int i=0; i<10; i++) { cin>&g.

邹九的个人博客 570

uni-app 实现高德 / 百度 / 腾讯地图多端导航跳转(H5最全方案)

本文介绍了一套生产级可复用的多地图导航Hook封装方案。针对开发中常见的经纬度顺序混乱、中文乱码、参数不统一等问题,提供了统一解决方案。方案支持高德、百度、腾讯三大地图平台,封装了各平台的URI参数处理逻辑,包括坐标顺序转换、名称编码、导航模式适配等核心功能。通过组合式API设计,开发者只需调用统一入口即可实现多地图导航跳转,同时保留单独调用各平台的能力。文中还总结了6大常见坑点,并推荐默认使用高德地图作为最稳定的导航方案。该方案适用于车辆定位、物流配送等多种业务场景,可封装为可复用的Hooks组件。

专注于前端开发与实战技巧,记录从零到一的学习与项目经验。分享 Vue3 / JavaScript / TypeScript / Flutter /Uniapp等前端技术,以及 性能优化、工程化、最佳实践。 537

Office 365中的Assigned Access详细功能介绍

本文系统介绍了Office365中AssignedAccess功能的应用与管理。作为Windows系统的重要安全特性,AssignedAccess可将设备锁定为单应用模式,有效防范未授权访问。文章详细阐述了其核心功能、技术架构及配置方法,包括UWP/Win32应用支持、多用户配置、与AzureAD集成等。通过MicrosoftEndpointManager可实现批量部署和远程监控,适用于公共终端、零售设备等场景。该功能为企业数字化转型提供了安全、可控的终端管理方案,在保障数据安全的同时优化用户体验。

xueyunshengling的博客 226

【PAT甲级 - C++题解】1098 Insertion or Heap Sort

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 484

机器人中的数值优化(十二)——带约束优化问题简介、LP线性规划

本系列文章主要是我在学习《数值优化》过程中的一些笔记和相关思考,主要的学习资料是深蓝学院的课程《机器人中的数值优化》和高立编著的《数值最优化方法》等,本系列文章篇数较多,不定期更新,上半部分介绍无约束优化,下半部分介绍带约束的优化,中间会穿插一些路径规划方面的应用实例

慕羽★的博客 3851

PAT 甲级 A1098

主要讲错误点: 算法笔记给出的一组用例: 4 3 4 2 1 3 4 2 1 //ouput Insertion Sort 2 3 4 1 可以用来检查自己的匹配插入排序的函数是否正确。有没有正确的处理初始序列和中间序列一致的情况。 代码如下: #include<iostream> #include<stdio.h> #include<stdlib.h> #i...

just_a_unicorn的博客 270

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【PAT 甲级】A1098 Insertion or Heap Sort (25分)

把堆排序和插入排序复现一遍就好了。 不过我个人认为的一个坑点是:对插入排序的处理要格外小心,要跳过对第一个点的插入排序才有可能拿到正确的结果。 比如一个测试样例: //Input 4 3 4 2 1 3 4 2 1 //Output 他应该输出的是 Insertion Sort 2 3 4 1 #include <bits/stdc++.h> using namespace std;...

weixin_42278063的博客 182

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

PAT 甲级 A1098 Insertion or Heap Sort

有n个元素,给出两个序列,第一个序列是初始序列,第二个序列是插入排序过程或者堆排序过程中的序列,问这个中间序列是那个排序的序列,并输出这次中间排序的下一次排列。思路模拟插入排序和堆排序,排列过程中一次次对比就好了。...

m0_51711089的博客 218

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

PAT甲级——A1098 Insertion or Heap Sort【25】

According to Wikipedia: Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data,...

weixin_42637495的博客 257

PAT甲级A1098 Insertion or Heap Sort (25 分)

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data,...

vir_lee的博客 301

【PAT甲级A1098】Insertion or Heap Sort (25分)(c++)

1098 Insertion or Heap Sort (25分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:400 ms 内存限制:64 MB According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remove

小庄同学的博客 199

pat 甲级 排序 A1098 Insertion or Heap Sort (25分)堆排序 插入排序

1098 Insertion or Heap Sort (25分) According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the locatio...

weixin_42358356的博客 164

PAT (Advanced Level) Practice A1098 Insertion or Heap Sort (25 分)(甲级)(C++)(堆排序)(插入排序)

原题链接:A1098 Insertion or Heap Sort #include&lt;algorithm&gt; #include&lt;iostream&gt; #include&lt;cstdio&gt; using namespace std; const int MAX = 110; int A[MAX], B[MAX], len; bool isSame()//判断A、B是否相...

izhxxx的博客 174

PAT程序设计考题——甲级1098(Insertion or Heap Sort ) C++实现

点击打开pat链接 #include #include #include #include #include #include #include #include using namespace std; #define INF 100000000 #define maxn 100010 int start[maxn],heap[maxn],object[maxn];

辛巴爱喝星巴克的博客 231

PAT甲级按题型分类

PAT甲级按题型分类 1. 水题篇 2. 数据结构篇 2.1 线性表 结构框架 顺序结构:顺序表 链式结构:单链表、双链表、循环链表(指针);静态链表(借助数组) 2.2 栈&队 结构框架 栈:顺序栈、链栈、共享栈 队列:循环队列、链式队列、双端队列 数组(推广):一维数组、对维数组(压缩、稀疏矩阵) 2.3 串 结构框架 基本功概念:主串、子串、串长 存储结构:定长顺序存储、堆分配存储、块链存储 模式匹配算法:暴力匹配、KMP算法(部分匹配值表、next数组、next函数的推理)、kmp

TypicalSpider的博客 3939

PAT甲级1098

PAT甲级1098 题目: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the s

zerohawk的博客 145

PAT甲级部分python解题记录

用Python写PAT甲级,答案都在这了。每一道题都有原题、题目大意以及代码实现,大部分的题目都有详细的逻辑分析。欢迎大家一起来讨论!

贰拾壹 4479

PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

1098Insertion or Heap Sort(25分) According to Wikipedia: Insertion sortiterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort remo...

小熊猫的博客 504
上一篇: PAT甲级 A1089
下一篇: PAT甲级 A1099
Kikkos
博客等级 码龄7年 5粉丝 77原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值