一、技术总结

  1. 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现
  2. 可以知道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 分)的更多相关文章

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

  2. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  3. 09-排序2 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. 1089 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  5. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

  6. 1089 Insert or Merge (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  8. PAT 1089. Insert or Merge (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. 1089. Insert or Merge (25)

    题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...

随机推荐

  1. Ubuntu 修改默认编辑器

    sudo update-alternatives --config editor

  2. Python连载6-time包函数简介

    一.接连载5中time模块 1.函数:altzone (1)含义:获取当前时间与UTC时间相差的秒数,再有夏令时的情况下. (2)格式:time.altzone 2.函数:daylight (1)含义 ...

  3. 【OCR技术系列之二】文字定位于切割

    要做文字识别,第一步要考虑的就是怎么将每一个字符从图片中切割下来,然后才可以送入我们设计好的模型进行字符识别.现在就以下面这张图片为例,说一说最一般的字符切割的步骤是哪些. 当然,我们实际上要识别的图 ...

  4. 05-Django模型(2)

    1.特殊查询 F和Q查询: 之前的查询都是模型对象的属性与常量值比较,两个属性怎么比较呢?使用F查询. F查询语法: from django.db.models import F F('属性名称') ...

  5. 解决SVN 被锁且Cleanup无效问题

    开发两年多,依然用svn做代码管理工具,看到隔壁java组用git,心向往之,奈何苦苦不得机会,既然用svn,那么就说一说svn碰到的问题如何解决吧. 有时候我们在提交,或者更新代码时,由于网络或其他 ...

  6. 在wcharczuk/go-chart图表上打印文字

    先看效果: 源码 package main import (    "bytes"    "fmt"    "io/ioutil"    & ...

  7. wp.editor.initialize 配置案例

    wp.editor.initialize ( 'EditorTextArea' , { tinymce: { wpautop: to true , theme: 'modern' , skin: 'l ...

  8. JS是解释型还是编译型语言?

    解释型和编译型语言 解释型语言 解释型语言是对代码进行一句一句的直接运行,在程序运行期间,使用解释器动态将代码解释为机器码,再运行. 编译型语言 编译型语言是需要使用编译器先对代码进行编译为机器码,再 ...

  9. 【微信小程序】e.currentTarget和e.target

    什么是事件 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 事件可以绑定在组件上,当达到触发事件,就会执行逻辑层中对应的事件处理函数. 事件对象可以携带额外信息,如 id ...

  10. Android开源日志框架xlog

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/144 xlog的优点 在开发过程中,避免不了要使用日志组件 ...