博客
关于我
程序设计基础54 two_pointers PAT A1089
阅读量:386 次
发布时间:2019-03-05

本文共 3950 字,大约阅读时间需要 13 分钟。

1089 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:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort1 2 3 5 7 8 9 4 6 0

Sample Input 2:

103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort1 2 3 8 4 5 7 9 0 6

一,注意点

1,注意可能有目标序列与初始序列一致的情况,但是题目中说了初始序列不参与比较,所以必须排除未开始比较初始序列就与目标序列一致的情况!

二,我的代码

#include
#include
using namespace std;const int max_n = 110;int arr[max_n], copy_arr[max_n], target[max_n], ans[max_n];void copy_same(int A[], int B[], int len) { for (int i = 0; i < len; i++) { B[i] = A[i]; }}bool is_same_arr(int A[], int B[], int len) { for (int i = 0; i < len; i++) { if (A[i] != B[i]) { return false; } } return true;}void print_arr(int A[], int len) { for (int i = 0; i < len; i++) { printf("%d", A[i]); if (i != len - 1) { printf(" "); } }}bool insert_sort(int A[], int len) { bool flag = false; int num = 0; for (int i = 1; i < len; i++) { if (flag == false) { if (i != 1 && is_same_arr(A, target, len)) { flag = true; } int temp = A[i]; int j = i; while (j > 0 && A[j - 1] > temp) { A[j] = A[j - 1]; j--; } A[j] = temp; if (flag == true) { return flag; } } } return flag;}void merge(int A[], int R1, int L1, int R2, int L2) { int i = R1, j = R2; int index = 0; int temp[max_n]; while (i <= L1&&j <= L2) { if (A[i] <= A[j])temp[index++] = A[i++]; else temp[index++] = A[j++]; } while (i <= L1)temp[index++] = A[i++]; while (j <= L2)temp[index++] = A[j++]; for (int i = 0; i < index; i++) { A[R1 + i] = temp[i]; }}bool merge_sort(int A[],int len) { bool flag = false; for (int step = 2; step / 2 < len; step *= 2) { if (flag == false) { if (step != 2 && is_same_arr(A, target, len)) { flag = true; } for (int i = 0; i < len; i += step) { int mid = i + step / 2 - 1; if (mid + 1 < len) { merge(A, i, mid, mid + 1, min(i + step - 1, len - 1)); } } if (flag == true) { return flag; } } } return flag;}int main() { int N = 0; bool flag = false; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &arr[i]); } for (int i = 0; i < N; i++) { scanf("%d", &target[i]); } copy_same(arr, copy_arr, N); flag = insert_sort(copy_arr, N); if (flag == true) { printf("Insertion Sort\n"); print_arr(copy_arr, N); } else { copy_same(arr, copy_arr, N); printf("Merge Sort\n"); flag = merge_sort(copy_arr, N); print_arr(copy_arr, N); } return 0;}

 

转载地址:http://illwz.baihongyu.com/

你可能感兴趣的文章
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>