https://pintia.cn/problem-sets/994805260223102976/problems/994805286714327040
这道题有点麻烦了
/解题思路: 建立归并和插入排序函数; 把目标数组分别用俩种排序排一次 对初始序列进行排序, 每排序一次,就与目标序列比较一次,若符合目标序列, 则,即我们要输出再一次排序后的数组/
#include <iostream> #include <algorithm> using namespace std; //num初始数列,aim目标数列,result输出数列 int n, result[100] = {0}, num[100] = {0}, aim[100] = {0}; int same(int a[], int b[]){ for(int i = 0; i < n; i++){ while(a[i] != b[i]) return 0; } return 1; } void show(int a[]){ cout << a[0]; for(int i = 1; i < n; i++) cout << " " << a[i]; } bool Insertion(){ bool flag = false; for(int i = 1; i < n; i++){ if(i != 1 && same(aim, result)) flag = true; int tmp = result[i], j = i; while(tmp < result[j-1] && j > 0){ result[j] = result[j-1]; j--; } result[j] = tmp; if(flag){ cout << "Insertion Sort" << endl; return true; } } return false; } void Merge(){ bool flag = false; for(int step = 2; step / 2 < n; step *= 2){ if(step != 2 && same(num, aim)) flag = true; //模拟归并的过程 并不是归并 for(int i = 0; i < n; i += step) sort(num+i, num+min(i+step, n)); if(flag){ cout << "Merge Sort" << endl; show(num); break; } } } int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> num[i]; result[i] = num[i]; } for(int i = 0; i < n; i++) cin >> aim[i]; if(Insertion()) show(result); else Merge(); return 0; }