根据维基百科的定义:

插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。

归并排序进行如下迭代操作:首先将原始序列看成 N 个只包含 1 个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下 1 个有序的序列。

现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究竟是哪种排序算法?

输入格式:

输入在第一行给出正整数 N (≤100);随后一行给出原始序列的 N 个整数;最后一行给出由某排序算法产生的中间序列。这里假设排序的目标序列是升序。数字间以空格分隔。

输出格式:

首先在第 1 行中输出Insertion Sort表示插入排序、或Merge Sort表示归并排序;然后在第 2 行中输出用该排序算法再迭代一轮的结果序列。题目保证每组测试的结果是唯一的。数字间以空格分隔,且行首尾不得有多余空格。

输入样例 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

输出样例 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

输入样例 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

输出样例 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
const int maxn = ; int main(){
int n;
int pre[], tar[], tmp[];
scanf("%d", &n);
for (int i = ; i < n; i++){
scanf("%d", &pre[i]);
tmp[i] = pre[i];
}
for (int i = ; i < n; i++){
scanf("%d", &tar[i]);
}
int i;
for (i = ; i < n; i++){
sort(tmp, tmp + i + );
if (equal(tmp, tmp + n, tar)){
printf("Insertion Sort\n");
sort(tmp, tmp + i + );
for (int j = ; j < n; j++){
printf("%d", tmp[j]);
if (j != n - )printf(" "); }
system("pause");
return ;
}
}
for (i = ; i < n; i=i*){
for (int j = ; j < n ; j+=i){
sort(pre + j, j + i < n ? pre + j + i : pre + n);
if (equal(pre, pre + n, tar)){
printf("Merge Sort\n");
i = i * ;
for (int j = ; j < n ; j += i){
sort(pre + j, j + i<n ? pre + j + i : pre + n);
}
for (int j = ; j < n; j++){
printf("%d", pre[j]);
if (j != n - )printf(" ");
}
system("pause");
return ;
}
}
}
/*
for (i = 2; i < n; i=i*2){
for (int j = 0; j*i < n ; j++){
sort(pre + j*i, (j +1)* i < n ? pre + (j +1)* i : pre + n);
if (equal(pre, pre + n, tar)){
printf("Merge Sort\n");
i = i * 2;
for (int j = 0;j*i < n ; j++){
sort(pre + j*i, (j +1)* i < n ? pre + (j +1)* i : pre + n);
}
for (int j = 0; j < n; j++){
printf("%d", pre[j]);
if (j != n - 1)printf(" ");
}
system("pause");
return 0;
}
}
}
*/
system("pause");
}

注意点:可以用 sort 和 equal 来判断排序情况,要注意归并排序不能越界,归并写法注释的那种比较复杂,不推荐

PAT B1035 插入与归并 (25 分)的更多相关文章

  1. PAT 1035. 插入与归并(25)

    根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...

  2. 1035 插入与归并 (25 分)C语言

    根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭 ...

  3. PAT (Basic Level) Practise (中文)-1035. 插入与归并(25)

    PAT (Basic Level) Practise (中文)-1035. 插入与归并(25)   http://www.patest.cn/contests/pat-b-practise/1035 ...

  4. PAT-乙级-1035. 插入与归并(25)

    1035. 插入与归并(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 根据维基百科的定义: 插入排序是迭 ...

  5. PAT 1035 插入与归并(25)(代码+思路+测试点分析)

    1035 插入与归并(25 分) 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到 ...

  6. 【算法笔记】B1035 插入与归并

    1035 插入与归并 (25 分) 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直 ...

  7. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  8. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  9. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

随机推荐

  1. while和if的区别

    while用于循环语句,而if用于判断和分支语句.由于你并没有指明是什么程序,只能泛泛而谈了.if 语句中,常用格式为:if(判断条件){执行语句}上面的结构,只是进行一次判断.if与else结合,就 ...

  2. Android组件化搭建

    什么是组件化 为了降低项目耦合性,在Android工程中如何实施,目前有两种途径,也是两大流派,一个是组件化,一个是插件化.在这里只介绍组件化,插件化暂不介绍 正常的APP只有一个applicatio ...

  3. Linux常用基本命令( touch )

    touch命令: 作用:创建空文件,或者改变文件的时间戳属性 格式: touch [option] [file] 1,同时创建一个或者多个空文件 ghostwu@dev:~/linux/mkdir$ ...

  4. H5添加禁止缩放功能

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scal ...

  5. Javascript经典算法学习1:产生随机数组的辅助类

    辅助类 在几个经典排序算法学习部分,为方便统一测试不同算法,新建了一个辅助类,主要功能为:产生指定长度的随机数组,提供打印输出数组,交换两个元素等功能,代码如下: function ArraySort ...

  6. nodejs 通过nginx后出现响应慢的解决方法

    最近用了nodejs搭建服务器,然后用了nginx做了反向代理,项目开发需求,没办法.但是发现了经过代理之后发现网页请求变慢了,而且是不能忍的一分钟以上. 一开始,怀疑是在nodejs那边的问题,结果 ...

  7. HTML:target=_blank、target=_top、target=_parent、target=_self 的区别

    HTML:target=_blank.target=_top.target=_parent.target=_self 的区别 _blank 在新窗口中打开链接_parent 在父窗体中打开链接_sel ...

  8. git 常见命令,规范 整理

    move commit to stage area(把本地的1个commit还原到 暂存区) git reset --soft HEAD~1 把其他的commit的合并到现在到分支:git cherr ...

  9. Flutter应用打包发布

    本文学习Flutter打包,打包环境,Android studio3.2,打包的程序就使用上文的酷炫天气预报 terminal执行下列命令: 1.生成key(如果有现成的Key跳过这一步)  2.ke ...

  10. Android studio button 按钮 四种绑定事件的方法

    package com.geli_2.sujie.sujiegeili2testbutton; import android.os.Bundle; import android.support.v7. ...