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, ...
随机推荐
- UmiJS 目录及约定
在文件和目录的组织上,umi 更倾向于选择约定的方式. 一个复杂应用的目录结构如下: . ├── dist/ // 默认的 build 输出目录 ├── mock/ // mock 文件所在目录,基于 ...
- swoole4创建Mysql连接池
一 .什么是mysql连接池 场景:每秒同时有1000个并发,但是这个mysql同时只能处理400个连接,mysql会宕机. 解决方案:连接池,这个连接池建立了200个和mysql的连接,这100 ...
- source vimrc的时候报错:.vimrc:1: command not found: syntax
vim的配置如下: 1 syntax enable //语法高亮 2 set number //显示行号 3 set cursorline //突出显示当前行 4 set ruler //打开状态栏标 ...
- 自动编写Python程序的神器,Python 之父都发声力挺!
就在不久前,kite——那个能够自己编写python代码的AI,Python 之父 Guido van Rossum 使用之后,也发出了「really love」感叹,向大家墙裂推荐了这一高效工具 ...
- Java队列和定时器Timer
一: Queue详解 Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构 Queue接口与List.Set同一级别,都是继承了Collection接口.Linked ...
- ASP.NET Core使用Nacos作为配置中心的多环境问题
前言 双11那天离职后,这段时间都待在家里,看看书,写写代码,逛逛招聘网站 周一去Gworld面试的时候,有听到面试官说他们用到了配置中心Apollo,聊下来,听他的意思,大概是处理了多环境这个比较方 ...
- Revit二次开发 屏蔽复制构件产生的重复类型提示窗
做了很久码农,也没个写博客的习惯,这次开始第一次写博客. 这个问题也是折腾了我接近一天时间,网上也没有任何的相关博文,于是决定分享一下,以供同样拥有此问题的小伙伴们参考. 内容源于目前在做的一个项目, ...
- python验证码识别(2)极验滑动验证码识别
目录 一:极验滑动验证码简介 二:极验滑动验证码识别思路 三:极验验证码识别 一:极验滑动验证码简介 近些年来出现了一些新型验证码,不想旧的验证码对人类不友好,但是这种验证码对于代码来说识别难度上 ...
- Java日期时间API系列7-----Jdk8中java.time包中的新的日期时间API类的特点
1.不变性 新的日期/时间API中,所有的类都是不可变的,这对多线程环境有好处. 比如:LocalDateTime 2.关注点分离 新的API将人可读的日期时间和机器时间(unix timestamp ...
- Winform中怎样获取项目图片资源并转换为Image对象
场景 DevExpress的TreeList怎样给树节点设置图标: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10274554 ...