编程题之合并两个有序的数组
给定两个有序的数组,试将其合并后的数组打印出来。。。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void arr_merge(int arr1[], int arr2[], int n1, int n2)
{
int arr[200];
int i=0;
int j=0;
int k=0;
while((i<n1)&&(j<n2))
{
if(arr1[i]<=arr2[j])
{
arr[k]=arr1[i];
i++;
k++;
}
else
{
arr[k]=arr2[j];
j++;
k++;
}
}
while(i<n1)
{
arr[k++]=arr1[i++];
}
while(j<n2)
{
arr[k++]=arr2[j++];
}
cout<<"The result " << endl;
for(int p=0;p<k;p++)
cout<<arr[p]<<" ";
cout<<endl;
}
int main()
{
int a1[100];
int a2[100];
int n1;
int n2;
cout<<"input the arr1 num : " << endl;
cin>>n1;
for(int i=0;i<n1;i++)
{
cin>>a1[i];
}
cout<<"input the arr2 num: " << endl;
cin>>n2;
for(int i=0;i<n2;i++)
{
cin>>a2[i];
}
sort(a1,a1+n1);
sort(a2,a2+n2);
arr_merge(a1, a2, n1, n2);
system("pause");
return 0;
}