非诚勿扰吴桐结婚了吗:设有已按从小到大顺序排列的数组A、B,将他们合并成一个从小到大顺序排列的数组C。

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/01 12:39:07

不知道你是用什么语音怎么答?
先吧两个数组组合成数组c,然后再排序就可以了,,

cv

挺简单啊,将A或者B其中一个数组复制出来,两个两相比较排序,就成了

//我用C++写的程序,不知符合不符合你的要求

#include <iostream.h>

//define two arrays
int A[3] = {1,4,8};
int B[4] = {2,4,5,9};

int AppendByOrder(int a[],int b[],int m, int n,int c[])
{
//m indicate the number of a's elements
//also n indicate the number if b 's elements

int total = m + n;
//c = new int[total];
int p = 0;
int q = 0;
for (int i = 0; i < total;i++)
{
if(a[p]<b[q]){
c[i] = a[p++];
if( p == m)
break;
}
else
{
c[i] = b[q++];
if(q == n)
break;
}
}
i++;
if(p==m)
for(;i<total;i++)
c[i] = b[q++];
if(q==n)
for(;i<total;i++)
c[i] = a[p++];
return 1;//successful
}

int Print(int a[],int n)
{
for(int i = 0;i<n;i++)
cout<<a[i]<<'\t';
return 1;
}

int main()
{
int C[10];
AppendByOrder(A,B,3,4,C);
Print(C,3+4);
return 0;
}

buaa_sheng 写得很不错,把分数给他!不过不够通用,何必去除动态数组呢?