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. Shogun网站上的关于主流机器学习工具包的比较

    Shogun网站上的关于主流机器学习工具包的比较: http://www.shogun-toolbox.org/page/features/   created last updated main l ...

  2. thinkPHP环境搭建小记

    php一直以来都被人诟病,说什么设计得很糟糕,有种你别用啊,不然就别bb了.最近,森哥在去年暑假学习了php基础和mvc模式的基础上准备用尝试一下国产ThinkPHP框架. 1.搭建LAMP环境 我实 ...

  3. 在亚马逊amazon的AWS上安装Node和MongoDB服务器

    在亚马逊amazon的AWS上安装Node和MongoDB服务器 在建立AWS上账号.创建EC2 ,用putty链接上之后,就可以用下面的方法开始安装. !!! 如果不是是自己建立的EC2, 而是由B ...

  4. shell 中调用其他的脚本

    方法有三种: 1 使用source 2 使用 . 3 使用sh 简单实验: first.sh #!/bin/bashecho 'your are in first file' second.sh #! ...

  5. urlparse

    urlparse模块 urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用,而且只在python2.7中存在,python3中是在urllib包下的urllib. ...

  6. iptables--简单的防火墙

    iptables--简单的防火墙 如果你执行iptables --list你将看到防火墙上的可用规则.下例说明当前系统没有定义防火墙,你可以看到,它显示了默认的filter表,以及表内默认的input ...

  7. 阿里云 esc 云服务器无缘无故的一次/usr/bin 目录丢失导致整个服务无法启动 # ./shutdown.sh ./shutdown.sh: line 41: dirname:command not found cannot find /catalina.sh the find /catalina.sh The file is absent or does not have execute

    总结上个星期服务器环境上的一个问题,一直再忙AR.防近视的项目没时间整理.刚好忙完项目认真回顾8月30发生的一个让人奇葩的问题. 早上把项目上的一些问题优化完,快到中午吃饭的时间频繁的启动导致/usr ...

  8. icePDF去水印方法

    原文:http://www.cnblogs.com/pcheng/p/5711660.html 1.首先下载到icepdf的架包. 2.用解压缩软件,对该jar文件进行解压,得到名为icepdf-co ...

  9. leetcode 206

    206. Reverse Linked List Reverse a singly linked list. 翻转一个单链表. 代码如下: /** * Definition for singly-li ...

  10. C语言dsPIC / PIC24 serial bootloader和C#语言bootloader PC端串口通信程序

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 新dsPIC/PIC2 ...