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的更多相关文章

  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. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  5. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  6. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  7. pat1089. Insert or Merge (25)

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

  8. Insert or Merge

    7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...

  9. PAT_A1089#Insert or Merge

    Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...

随机推荐

  1. Android开发-Android Studio使用问题解决

    回头一看,很久没来更新了,归其原因,还是懒癌发作,倒是生活作息规律了,几乎每天都在11点前休息.今天趁着培训,使用android studio,发现几个坑: 1.android studio每次都提示 ...

  2. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  3. 好用的一个从SharePoint导出小工具

      1. 输入 Site Url(Site Collection), 然后点"load"按钮 2.选择Web后,点选需导出的文档库,然后点"Next"按钮   ...

  4. 转:Java.file

    类 java.io.File 的使用 使用 File 的软件包 java.awt 包含用于创建用户界面和绘制图形图像的所有类. java.io 通过数据流.序列化和文件系统提供系统输入和输出. jav ...

  5. openwrt编译环境搭建

    1,首先安装ubuntu系统,这里安装的是虚拟机 2,安装openwrt编译所需环境  apt-get install build-essential libncures5-dev gawk libs ...

  6. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];

    题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...

  7. 线程Thread

    一.线程: 程序:保存在物理介质(光盘,软盘,硬盘)当中的代码片段 进程:一旦程序运行起来,就变成了操作系统当中的一个进程 线程:程序当中一条独立执行的线索 二.线程的五大状态 新生    就绪 运行 ...

  8. 【原创】android内存管理-hprof文件

    转载请注明出处 http://www.cnblogs.com/weiwangnuanyang/p/5703702.html 如果只是想确定一下某一个场景是否有内存泄漏,AndroidStadio的控制 ...

  9. 1o_Samba

    ∮Linux下Samba的安装配置 §Linux 下文件共享软件 Samba 是一款实现跨主机,跨平台文件共享的软件.同类型的软件还有 ftp,nfs 不再赘述. §文件共享协议 Windows : ...

  10. 用CMake设置Visual Studio工程中预处理器定义值

    构建VS工程时预处理值是不可缺少的,如动态库的导出配置等.在通过CMake构建VS工程时,可以通过CMake命令进行定义,下面讲三种应用. 字符集:默认装填下VS工程是多字节字符集,如果需要使用Uni ...