PTA Insert or Merge
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 NN (\le 100≤100). Then in the next line, NN integers are given as the initial sequence. The last line contains the partially sorted sequence of the NN 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
解答
这题目就是先用插入排序去尝试,每插入一次就比较与题目中的数组是否相等,如果相等的话就再进行一次插入然后输出(用flag变量来控制,相等就置为true,每一次插入后检查flag为true就跳出循环),如果全部排序完成还没有相等的,就说明是归并排序,这里隐含条件是用迭代的归并排序,同样每一次都与题中数组比较,相等后就再归并一次。
//
// main.c
// Insert or Merge
//
// Created by 余南龙 on 2016/12/9.
// Copyright © 2016年 余南龙. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define MAX 101
int comp(const void *a, const void *b){
return *(int*)a - *(int*)b;
}
void Init(int initial[], int tmp[], int partial[], int *N){
int i, tmp_val;
scanf("%d", N);
tmp_val = *N;
; i < tmp_val; i++){
scanf("%d", initial + i);
tmp[i] = initial[i];
}
; i < tmp_val; i++){
scanf("%d", partial + i);
}
}
void Output(int a[], int size){
int i;
; i < size; i++) {
== i) {
printf("%d", a[i]);
}
else{
printf(" %d", a[i]);
}
}
}
int Is_Equal(int a[], int b[], int size){
int i;
; i < size; i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
int Is_Insertion_Sort(int initial[], int partial[], int size){
;
; i < size; i++){
tmp = initial[i];
; j >= ; j--){
if(initial[j] > tmp){
initial[j + ] = initial[j];
}
else{
break;
}
}
initial[j + ] = tmp;
== flag){
return true;
}
if(Is_Equal(initial, partial, size)){
flag = ;
}
}
return false;
}
void New_Merge_Sort(int initial[], int partial[], int size){
int i, j, flag = false;
; i < size; i *= ){
; j < size; j += i * ){
< size)
qsort(initial + j, i * , sizeof(int), comp);
else if(size - j > i){
qsort(initial + j, size - j, sizeof(int), comp);
}
}
if(flag){
break;
}
if(Is_Equal(initial, partial, size)){
flag = true;
}
}
}
int main(){
int N;
int initial[MAX], partial[MAX], tmp[MAX];
Init(initial, tmp, partial, &N);
if(!Is_Insertion_Sort(initial, partial, N)){
New_Merge_Sort(tmp, partial, N);
printf("Merge Sort\n");
Output(tmp, N);
}
else{
printf("Insertion Sort\n");
Output(initial, N);
}
}
PTA Insert or Merge的更多相关文章
- 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 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT1089. Insert or Merge
PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- Insert or Merge
7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...
- PAT_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
随机推荐
- springmvc 接受特殊类型字段的处理方法
springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下. 1.在需要处理的字段前加上注解: @DateTimeForma ...
- Norflash控制器的Verilog建模之一
摘要:今天驱动一款SPANSION公司生产的norflash——S29AL032D70,没有别的参考资料,大致了解一下norflash的内部cmos电路架构以及其用途之后,直接看手册吧. 如何看手册: ...
- 用javascript比较快速排序和合并排序的优劣
<script> //用来调用排列方法的类 function arr_sort(arr){ var startTime,endTime; var priv_arr = new Array; ...
- EF4.1使用
EF分为三类: db first:首先建立数据库,然后通过ADO.Net Entity Data Model项目建立.edmx文件,这是一个xml文件主要作用就是映射类和数据表 model first ...
- vs2010通过添加资源文件,修改win32创建的窗口
1.右键项目的“资源”,新建资源,生成.rc文件 2.进入资源视图创建icon或menu等资源 3.退出资源视图,编译这个.rc文件 4.项目源代码中添加如下几个头文件 #include " ...
- SQLyog-直接导出JSON格式的数据
前言:以前做过的一个项目,有这样的一个需求使用搜索引擎来查询对应的区域信息,不过区域信息要先导出来,并且数据格式是JSON格式的,在程序中能实现这个需求,不过下面的这种方法更加的简单,通过 ...
- jquery让滚动条跳到最底部
selector.scrollTop(50000); 添加一个最大的数值: 或者 公式:内容器的高度加上外层容器的padding,再减去外层容器的高度: var tableHeight = $(' ...
- FreeMark学习(一)
FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用程序 虽然FreeMark ...
- 使用ab进行压力测试
在Windows系统的命令行下,进入ab.exe程序所在目录,执行ab.exe程序.注意直接双击无法正确运行.
- 阅读Python官方文档心得
我会每天都阅读一些python的官方文档,并每天更新心得体会. -------------------------------------------------2016.12.08--------- ...