Combine
// author: montaguehu
// email: montaguehu@hotmail.com
// cs.sjtu.edu.cn
#include <cstdlib>
#include <iostream>
using namespace std;
int* combine(int* array1,int array1_size,int* array2,int array2_size)
{
int result_size=array1_size+array2_size;
int* result=new int[result_size];
int i=0,j=0;
int array_i=0;
while (i<array1_size && j<array2_size)
{
if (array1[i]<=array2[j])
{
result[array_i++]=array1[i++];
}
else
{
result[array_i++]=array2[j++];
}
}
if (i==array1_size)
{
//move the rest of the array2 to the main array
for (int k=j;k<array2_size;++k)
{
result[array_i+k-j]=array2[k];
}
}
else
{
//move the rest of the array1 to the main array
for (int k=i;k<array1_size;++k)
{
result[array_i+k-i]=array1[k];
}
}
return result;
}
void main(int argc, char *argv[])
{
int array1[]={1,3,4,6};
int array2[]={2,4,5,11,80};
// two test cases
// first test case
cout<<"First test case"<<endl;
int* result1=combine(array1,4,array2,5);
for (int i=0;i<9;++i)
{
cout<<result1[i]<<endl;
}
cout<<endl;
delete result1;
//second test cases;
cout<<"Second test case"<<endl;
int* result2=combine(array2,5,array1,4);
for (i=0;i<9;++i)
{
cout<<result2[i]<<endl;
}
cout<<endl;
delete result2;
}
-------------------------------------------------------------------------------------------
这个家伙很懒,什么也没留下......