1089.Insert or Merge (25)...to be continued...
2017-02-24 19:50
459 查看
1089.Insert or Merge (25)…to be continued…
pat-al-1089
2017-02-24
乙级里面写过,优化了一下,原来还可以这样写
参考:《算法笔记:上机训练实战指南》机械工业出版社
-TBC-
pat-al-1089
2017-02-24
乙级里面写过,优化了一下,原来还可以这样写
参考:《算法笔记:上机训练实战指南》机械工业出版社
/** * pat-al-1089 * 2017-02-23 * Cpp version * Author: fengLian_s */ #include<stdio.h> #include<algorithm> #define MAX 110 using namespace std; int n, input[MAX], tmpInput[MAX], changed[MAX]; bool isSame(int a[], int b[]) { for(int i = 0;i < n;i++) { if(a[i] != b[i]) return false; } return true; } bool insertSort() { //printf("hello\n"); bool flag = false; for(int i = 1;i < n;i++) { if(i != 1 && isSame(tmpInput, changed)) flag = true; int tmp = tmpInput[i], j = i; while(j > 0 && tmpInput[j-1] > tmp) { tmpInput[j] = tmpInput[j-1]; j--; } tmpInput[j] = tmp; if(flag == true) return true; } return false; } void mergeSort() { bool flag = false; for(int step = 2;step / 2 <= n;step *= 2) { if(step != 2 && isSame(tmpInput, changed)) { flag = true; } for(int i = 0;i < n;i += step) { sort(tmpInput+i, tmpInput+min(i+step, n)); } if(flag == true) return; } } void printArray(int a[]) { printf("%d", a[0]); for(int i = 1;i < n;i++) { printf(" %d", a[i]); } putchar('\n'); } int main() { freopen("in.txt", "r", stdin); scanf("%d", &n); //printf("n = %d\n", n); for(int i = 0;i < n;i++) { scanf("%d", &input[i]); tmpInput[i] = input[i]; } for(int i = 0;i < n;i++) { scanf("%d", &changed[i]); //printf("%d ", changed[i]); } //putchar('\n'); if(insertSort()) { printf("Insertion Sort\n"); printArray(tmpInput); } else { printf("Merge Sort\n"); for(int i = 0;i < n;i++) { tmpInput[i] = input[i]; } mergeSort(); printArray(tmpInput); } return 0; }
-TBC-
相关文章推荐
- 1039.Course List for Student (25)...to be continued...
- 1082.Read Number in Chinese (25)...to be continued...
- 1071.Speech Patterns (25)...to be continued...
- 1075.PAT Judge (25)...to be continued...
- 1047.Student List for Course (25)...to be continued...
- 1017.Queueing at Bank (25)...to be continued...
- 1020.Tree Traversals (25)...to be continued...
- 1059.Prime Factors (25)...to be continued...
- 1067.Sort with Swap(0,*) (25)...to be continued...
- 1043.Is It a Binary Search Tree (25)...to be continued...
- 1024.Palindromic Number (25)...to be continued...
- 1044.Shopping in Mars (25)...to be continued...
- 1016.Phone Bills (25)...to be continued...
- 1056.Mice and Rice (25)...to be continued...
- 1060.Are They Equal (25)...to be continued...
- 1066.Root of AVL Tree (25)...to be continued...
- 1093.Count PAT's (25)...to be continued...
- 1010.Radix (25)...to be continued...
- 1012.The Best Rank (25)...to be continued...
- 1086.Tree Traversals Again (25)...to be continued...