PAT B1035 插入与归并 (25 分)

根据维基百科的定义:

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

归并排序进行如下迭代操作:首先将原始序列看成 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 = 100010;

int main(){
    int n;
    int pre[101], tar[101], tmp[101];
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        scanf("%d", &pre[i]);
        tmp[i] = pre[i];
    }
    for (int i = 0; i < n; i++){
        scanf("%d", &tar[i]);
    }
    int i;
    for (i = 1; i < n; i++){
        sort(tmp, tmp + i + 1);
        if (equal(tmp, tmp + n, tar)){
            printf("Insertion Sort\n");
            sort(tmp, tmp + i + 2);
            for (int j = 0; j < n; j++){
                printf("%d", tmp[j]);
                if (j != n - 1)printf(" ");

            }
            system("pause");
            return 0;
        }
    }
    for (i = 2; i < n; i=i*2){
        for (int j = 0; 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 * 2;
                for (int j = 0; j < n ; j += i){
                    sort(pre + j, j + i<n ? pre + j + i : pre + n);
                }
                for (int j = 0; j < n; j++){
                    printf("%d", pre[j]);
                    if (j != n - 1)printf(" ");
                }
                system("pause");
                return 0;
            }
        }
    }
    /*
    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 来判断排序情况,要注意归并排序不能越界,归并写法注释的那种比较复杂,不推荐

原文地址:https://www.cnblogs.com/tccbj/p/10363666.html

时间: 2024-08-10 16:43:14

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

PAT 1035. 插入与归并(25)

根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭代操作:首先将原始序列看成N个只包含1个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下1个有序的序列. 现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究竟是哪种排序算法? 输入格式: 输入在第一行给出正整数N (<=100):随后一行给出原始序列的N个整数:最后一

1035. 插入与归并(25)

1035. 插入与归并(25) 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭代操作:首先将原始序列看成N个只包含1个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下1个有序的序列. 现给定原始序列和由某排序算法产生的中间序列,请你判断该算法究竟是哪种排序算法? 输入格式: 输入在第一行给出正整数N (<=100):随后一

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

1035. 插入与归并(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭代操作:首先将原始序列看成N个只包含1个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下1个有序的序列. 现给定

Programming Ability Test学习 1035. 插入与归并(25)

1035. 插入与归并(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置.如此迭代直到全部元素有序. 归并排序进行如下迭代操作:首先将原始序列看成N个只包含1个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下1个有序的序列. 现给定

PAT乙级1085-----PAT单位排行 (25分)

1085 PAT单位排行 (25分) 输入样例: 10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypu 输出样例: 5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2 思路:(struct sc

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 1036 Boys vs Girls (25 分)

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai