首页 > 代码库 > 将两个数组A和B合并为一个有序的C数组

将两个数组A和B合并为一个有序的C数组

 1 # include<iostream> 2 # include<cstdio> 3 # include<algorithm> 4 using namespace std; 5 void Sort(int a[],int b[],int c[],int n,int m) 6 { 7     int A=0, B=0, C=0; 8     while(A<n && B<m) 9     {10         if(a[A] <= b[B])11             c[C++] = a[A++];12         else13             c[C++] = b[B++];14     }15     while(A<n)16         c[C++] = a[A++];17     while(B<m)18         c[C++] = b[B++];19     for(int x = 0; x < n+m; x++)20     {21         cout<<c[x]<<" ";22     }23 }24 int main()25 {26     int a[1000];27     int b[1000];28     int c[2000];29     int n,m;30     cin>>n>>m;31 32     for(int i = 0; i < n; i++)33     {34         cin>>a[i];35     }36     sort(a,a+n);37     for(int j = 0; j < m; j++)38     {39         cin>>b[j];40     }41     sort(b,b+m);42     Sort(a,b,c,n,m);43     return 0;44 }
View Code