#include <iostream> using namespace std; int merge2(int a[],int n,int b[],int m,int s[]) { int i=0,j=0; while(i<n&&j<m) { if(a[i]<b[j]) { s[i+j]=a[i]; i++; } else { s[i+j]=b[j]; j++; } } while(i<n) { s[i+j]=a[i]; i++; } while(j<m) { s[i+j]=b[j]; j++; } return 1; } int merge3(int *a,int num_a,int *b,int num_b,int *c,int num_c,int *d){ int p=0,q=0; int res[50]; merge2(a,num_a,b,num_b,res); // for (int i=0;i<num_a+num_b;i++) // { // cout<<res[i]<<" "; // } merge2(res,(num_a+num_b),c,num_c,d); return 0; } int main(){ int v1[]={32,43,46}; int v2[]={2,5,6,8,22,45}; int v3[]={3,9,21,30}; int v4[100]; int a=3,b=6,c=4; merge3(v1,a,v2,b,v3,c,v4); for (int i=0;i<a+b+c;i++) { cout<<v4[i]<<" "; } }
今天笔试睿初科技的一道编程题,三个归并没想到分成两个两个,好伤心