A1089 Insert or Merge (25 分)
一、技术总结
- 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现
- 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所以知道当发现有位置当前数比后面大时,进而再判断后面的每一位是否啊a、b数组都相等,如果是那么就是插入排序,再在后面进行一轮排序即可。如果不是就是归并排序。最主要的问题是判断归并排序到哪一轮了,使用while循环模拟归并排序,对数组a进行归并排序,直到与数组b相等,再进行一轮排序就是输出结果了。
二、参考代码
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[100], b[100], i, j, n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
}
for(i = 0; i < n-1 && b[i] <= b[i+1]; i++);
for(j = i+1; j < n && a[j] == b[j]; j++);
if(j == n){
printf("Insertion Sort\n");
sort(a, a+i+2);
}else{
printf("Merge Sort\n");
int flag = 1, k = 1;
while(flag){
flag = 0;
for(i = 0; i < n; i++){
if(a[i] != b[i]){
flag = 1;
}
}
k = k*2;
for(i = 0; i < n/k; i++){
sort(a+i*k, a+(i+1)*k);
}
sort(a+n/k*k, a+n);
}
}
for(j = 0; j < n; j++){
if(j != 0){
printf(" ");
}
cout << a[j];
}
return 0;
}
A1089 Insert or Merge (25 分)的更多相关文章
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
随机推荐
- 【转】Java中的关键字 transient
阅读目录 先解释下Java中的对象序列化 关于transient关键字 举个例子 参考资料 先解释下Java中的对象序列化 在讨论transient之前,有必要先搞清楚Java中序列化的含义: Jav ...
- C#开发BIMFACE系列29 服务端API之获取模型数据14:获取图纸列表
系列目录 [已更新最新开发文章,点击查看详细] 一个三维模型中可能包含对应多张二维图纸列表,本篇主要介绍如何获取模型文件对应的图纸列表. 请求地址:GET https://api.bimfac ...
- HTML连载49-清除浮动的第三种方式(内外墙法)
一.清除浮动的第三种方式 1.隔墙法有两种如下:外墙法和内墙法. 2.外墙法 (1)在两个盒子中间添加一个额外的块级元素 (2)给这个额外添加的块级元素设置:clear:both;属性 注意点: ...
- Spring的增强模式
一.前置增强 1.IdoSomeService 2.IdoSomeServiceImpl类实现IdoSomeService接口 3.MyBeforeAdvice 实现前置增强方法 4.applicat ...
- 【STM32H7教程】第19章 STM32H7的GPIO应用之按键FIFO
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第19章 STM32H7的GPIO应用之按键FIF ...
- python接口自动化11-pytest入门
前言 pytest是一个非常成熟的全功能的Python测试框架,适合从简单的单元到复杂的功能测试,主要特点有以下几点: 简单灵活,容易上手: 支持参数化: 能够支持简单的单元测试: 标记测试功能与属性 ...
- 企业如何做好B2C电商平台
导语本文主要讲了两个方面:1.企业如何定位B2C电商平台:2.企业做B2C遇到的问题. 一.企业如何定位B2C电商平台传统企业做B2C电子商务一般选用品牌.渠道.平台模式这三种,品牌模式是在网上建立一 ...
- D - Ugly Problem HDU - 5920
D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...
- Alpine Linux 安装 lxml Pillow 失败
lixm 需要编译安装,因此需要先安装gcc g++ RUN apk add --update --no-cache g++ gcc libxslt-dev python3-dev openssl-d ...
- windows 安装使用jupyter及 基础配置
jupyter 是什么Jupyter Notebooks 是一个交互式笔记本,支持运行 40 多种编程语言,它的本质是一个 开源的 Web 应用程序,我们可以将其用于创建和共享代码与文档,他可以支持实 ...