09-排序2 Insert or Merge (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.
Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.
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 "Merge 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 resuling 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 0 6
1 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int origin[maxn],tmpOri[maxn],change[maxn]; bool Insertion(int n);
void showArray(int arr[], int n);
bool Issame(int A[], int B[], int n);
void merge(int n); int main()
{
int n;
scanf("%d",&n); for (int i = ; i < n; i++)
{
scanf("%d",&origin[i]);
tmpOri[i] = origin[i];
}
for (int i = ; i < n; i++)
{
scanf("%d",&change[i]);
} if (Insertion(n))
{
printf("Insertion Sort\n");
showArray(tmpOri,n);
}
else
{
printf("Merge Sort\n");
for (int i = ; i < n; i++)
{
tmpOri[i] = origin[i];
}
merge(n);
} return ;
} bool Insertion(int n)
{
bool flag = false;
for (int i = ; i < n; i++)
{
if (i != && Issame(tmpOri, change, n))
{
flag = true;
} int tmp = tmpOri[i];
int j = i;
while (j >= && tmpOri[j - ] > tmp)
{
tmpOri[j] = tmpOri[j-];
j--;
}
tmpOri[j] = tmp; if (flag)
{
return flag;
}
}
return false;
} void showArray(int arr[], int n)
{
for (int i = ; i < n; i++)
{
printf("%d", arr[i]);
if (i < n - )
{
printf(" ");
}
}
} bool Issame(int A[], int B[], int n)
{
for (int i = ; i < n; i++)
{
if (A[i] != B[i])
{
return false;
}
}
return true;
} void merge(int n)
{
bool flag = false;
for (int step = ; step / <= n; step *= )
{
if (step != && Issame(tmpOri, change, n))
{
flag = true;
} for (int i = ; i < n; i += step)
{
sort(tmpOri+i, tmpOri+min(i+step, n));
} if (flag)
{
showArray(tmpOri, n);
return ;
}
}
}
09-排序2 Insert or Merge (25 分)的更多相关文章
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 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 ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- 【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 ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 7-19(排序) 寻找大富翁 (25 分)(归并排序)(C语言实现)
7-19(排序) 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式 ...
- 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 ...
随机推荐
- 【排错】springboot项目,启动报An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist.
pom文件新引入: <dependency> <groupId>com.google.code.gson</groupId> ...
- C++中的双冒号作用
1. 作用域符号::的前面一般是类名称,后面一般是该类的成员名称,C++为例避免不同的类有名称相同的成员而采用作用域的方式进行区分如:A,B表示两个类,在A,B中都有成员member.那么 ...
- Redis(一) redis安装、启停
Redis是开源的内存数据存储,常被用作为内存数据库.缓存.全局队列.计数器等等. Redis安装 Redis分为多种模式:单机模式.高可用模式.集群模式.这篇中主要简介单机版的安装方式. 源码构建式 ...
- 10、VUE路由技术
1.前端路由 前端路由在很多开源的js类库框架中都得到支持,如AngularJS.Backbone.Vue.js等等. 前端路由和后端路由原理一样,是让所有的交互和展示在一个页面运行,以达到减少服务器 ...
- axios FastMock 跨域 代理
发送请求: 实现:发送请求,获取数据. 原本想自己写服务,后来无意间找到FastMock这个东东,于是就有了下文... 首先我安装了axios,在fastmock注册好了并创建了一个接口,怎么搞自行百 ...
- Tornado笔记
helloworld Tornado特点一句话简介:Tornado是非阻塞式的Web服务器,速度非常快,每秒可以处理数以千计的链接,因此Tornado是实时Web服务的一个理想框架.Tornado因为 ...
- 适合新手入门Spring Security With JWT的Demo
Demo 地址:https://github.com/Snailclimb/spring-security-jwt-guide .欢迎 star! Spring Security 是Spring 全家 ...
- LIBRARY_PATH和LD_LIBRARY_PATH
LIBRARY_PATH是编译时指定的路径. LD_LIBRARY_PATH是运行时指定的动态链接库所在目录. 在运行一个可执行文件之前,可以通过ldd a.exe命令查看a.exe所依赖的动态链接库 ...
- tcp / udp 协议及其实现的socket
一.tcp协议 1.1 基本知识 特点: 可靠,慢,全双工通信 建立连接时:三次握手 断开连接时:四次挥手 在建立起连接之后 发送的每一条信息都有回执 为了保证数据的完整性,还有重传机制 长连接:会一 ...
- <Android Studio> 4.Adapter的那些事 <一>
android 的表格显示和Windows桌面开发原理不同,其他平台转过来的同学要有心理准备,不要拿桌面开发的思维模式来思考android上的各种表格. 一.原理 此处使用ArrayAdapter来记 ...