pat 1089 Insert or Merge
来源:互联网 发布:照片恢复软件免费版 编辑:程序博客网 时间:2024/06/04 19:10
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 <stdio.h>#include <string.h>int n;int num[110], numdp[110], num2[110];int cntdp = 0; //void swap(int &a, int b){int tmp = b;b = a;a = tmp;}int insertSort(){int i, j, index;bool flag = false;for(i = 2; i <= n; i++){index = i;for(j = i - 1; j >= 1; j--){if(num[i] < num[j]){index = j;} else if(num[j] <= num[i])break;}if(index != i){int tmp = num[i];for(j = i; j > index; j--){int tmp = num[j];num[j] = num[j-1];num[j-1] = tmp;}}if(flag == true)return 1;flag = false;for(j = 1; j <= n; j++){if(num[j] == numdp[j])continue;else{break;}}if(j >= n){flag = true;}}return 0;}void outPut(){int i;for(i = 1; i < n; i++)printf("%d ", num[i]);printf("%d\n", num[n]);}bool flagm = false;/*int mergeSort(int a, int b){int i, j, k;for(j = 1; j <= n; j++){if(num[j] == numdp[j])continue;elsebreak;}outPut();printf("j = %d\n", j);if(j > n)flagm = true;elseflagm = false;if(a >= b)return 0;int mid = (a + b) / 2;mergeSort(a, mid);mergeSort(mid+1, b);for(i = a, j = mid+1; i <= mid && j <= b;){if(num[i] <= num[j])i++;else if(num[i] > num[j]){int tmp = num[j];for(k = j; k > i; k--){num[k] = num[k-1];}num[i] = tmp;j++;mid++;i++;}}if(flagm == true){for(i = 1; i <= n; i++)printf("%d ", num[i]);printf("\n");return 1;}return 0;}*/void mergeSort(){int l = 1, i, j, k, r;int mid, end;flagm = false;int cnt = 0;while(2*l <= n){if(flagm) cnt = 1;for(i = 1; i <= n || n-i-2*l+1 >= 0; i = i + 2*l){if(n-i-2*l+1 >= 0){ mid = i+l-1, end = i+2*l-1;} else if(i <= n){mid = i+l-1, end = n;}//sort from num[i-i+l-1] to num[i+l-i+2*l-1]for(k = i, r = i+l; k <= mid && r <= end;){if(num[k] <= num[r]) k++;else if(num[k] > num[r]){int tmp = num[r];for(j = r; j > k; j--)num[j] = num[j-1];num[k] = tmp;k++;r++;mid++;}}for(k = 1; k <= n; k++){if(num[k]==numdp[k]) continue;else break;}if(k > n){flagm = true;}}if(cnt == 1) {outPut();return ;}l = l * 2;}for(i = 1, j = l+1; i <= l && j <= n;){if(num[i] <= num[j]) i++;else if(num[i] > num[j]){int tmp = num[j];for(k = j; k > i; k--)num[k] = num[k-1];num[i] = tmp;l++;i++;j++;}}outPut();}int main(){while(scanf("%d", &n) != EOF){int i;for(i = 1; i <= n; i++){scanf("%d", &num[i]);num2[i] = num[i];}for(i = 1; i <= n; i++)scanf("%d", &numdp[i]);int tmp = insertSort();if(tmp == 1){printf("Insertion Sort\n");outPut();} else {for(i = 1; i <= n; i++)num[i] = num2[i];printf("Merge Sort\n");mergeSort();}}return 0;}
- pat 1089 Insert or Merge
- PAT 1089. Insert or Merge
- 【PAT】1089. Insert or Merge
- Pat(Advanced Level)Practice--1089(Insert or Merge )
- PAT (Advanced Level) Practise 1089 Insert or Merge (25)
- PAT (Advanced Level) Practise 1089 Insert or Merge (25)
- 1089 Insert or Merge
- PAT 1089. Insert or Merge (25)
- PAT A 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-A1089. Insert or Merge (25)
- PAT 1061Insert or Merge (25)
- PAT(A) - 1089. Insert or Merge (25)
- pat-a1089. Insert or Merge (25)
- PAT A1089 insert or merge (25)
- HttpServletRequest
- android apk 数字证书 数字签名
- 第14周项目3带有姓名的多科成绩单
- Unity 3D学习笔记之一 界面介绍
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- pat 1089 Insert or Merge
- neutron的基本原理
- 安装卸载window服务
- Sql标识列
- mysql错误代码
- http的get和post区别
- 实习篇之补牙
- CSDN博客
- Android系统如何录制屏幕(录制成mp4格式)