pat 1089 Insert or Merge
2014-11-30 20:35
344 查看
1089. Insert or Merge (25)
时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueAccording 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:10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0Sample Output 1:
Insertion Sort 1 2 3 5 7 8 9 4 6 0Sample Input 2:
10 3 1 2 8 7 5 9 4 0 6 1 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort 1 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);}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; else break; } outPut(); printf("j = %d\n", j); if(j > n) flagm = true; else flagm = 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 (Advanced Level) Practise 1089 Insert or Merge (25)
- PAT (Advanced Level) Practise 1089 Insert or Merge (25)
- pat1089 Insert or Merge
- 1089. Insert or Merge (25)
- PAT 数据结构 07-排序2. Insert or Merge (25)
- 1089. Insert or Merge
- 1089. Insert or Merge (25)
- 1089.Insert or Merge
- 1089. Insert or Merge
- 浙大pat | 浙大pat 牛客网甲级1061 Insert or Merge (25)判断是插入排序还是归并排序
- 1089. Insert or Merge
- PAT A1089 insert or merge (25)
- 1089 Insert or Merge
- PAT 1061 Insert or Merge (25)
- 09-排序2 Insert or Merge
- 中国大学MOOC-数据结构基础习题集、07-2、Insert or Merge
- 中国大学MOOC-陈越、何钦铭-数据结构 Insert or Merge
- Insert or Merge(25 分)
- 排序算法的判别: Insert or Merge,Insertion or Heap Sort
- A soulation for loss the PageSetUp porperty after InsertFile or merge two files in Interp.Word