PAT-A-1089. Insert or Merge (25)
来源:互联网 发布:人力资源数据分析模型 编辑:程序博客网 时间:2024/06/05 02:39
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 resulting 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 0Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0Sample Input 2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort1 2 3 8 4 5 7 9 0 6
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[110], b[110], c[110];int n;void Show(int d[]){ int f = 1; for (int i = 0; i < n; i++) { if (f) { f = 0; cout << d[i]; } else cout << " " << d[i]; }}int Issame(int d[]){ for (int i = 0; i < n; i++) { if (d[i] != c[i]) return 0; } return 1;}int Insertsort(){ int num; int j; int flag = 0; for (int i = 1; i < n; i++) { if (i != 1 && Issame(a)) flag = 1; num = a[i]; j = i; while (j>0 && num < a[j - 1]) { a[j] = a[j - 1]; j--; } a[j] = num; if (flag == 1) return 1; } return 0;}int Mergesort(){ int flag = 0; for (int step = 2; step / 2 <= n; step *= 2) { if (step != 2 && Issame(b)) flag = 1; for (int i = 0; i < n; i += step) { sort(b + i, b + min(i + step, n)); } if (flag) return 1; }}int main(){ cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } for (int i = 0; i < n; i++) cin >> c[i]; if (Insertsort()) { cout << "Insertion Sort" << endl; Show(a); } else { cout << "Merge Sort" << endl; Mergesort(); Show(b); } system("pause"); return 0;}
- PAT A 1089. Insert or Merge (25)
- PAT(A) - 1089. Insert or Merge (25)
- PAT-A 1089. Insert or Merge (25)
- PAT-A-1089. Insert or Merge (25)
- Pat(A) 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- PAT(Advance) 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- 【PAT】1089. Insert or Merge (25)
- PAT 甲级 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge
- 【PAT】1089. Insert or Merge
- PAT 1088 1089. Insert or Merge (25)(排序啊)
- 1089. Insert or Merge (25)-PAT甲级真题
- [转]PAT甲级练习1089. Insert or Merge (25)
- 【PAT】【Advanced Level】1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25) 允许有重复元素!!!!!!!!!!!!!
- 欢迎使用CSDN-markdown编辑器
- Linux文件层级标准
- 一个菜鸟的算法导论学习笔记【Introsuction&Chapter 1】
- 重写
- 小项目第四天
- PAT-A-1089. Insert or Merge (25)
- JAVASE_基础
- Linux运维常用命令
- 在表单提交前进行验证的几种方式
- 图片预加载之延迟加载2-懒加载
- 大数阶乘
- Problem C: 不同交通工具的速度
- 调试魅族手机不显示通知栏消息bug的过程
- Raknet 库学习二