#include <iostream>
using namespace std;
/*
A,B为两个数组,且都是按从大到小的顺序排列的现在要将B合并到A里面
并且新数组A仍然按照顺序排列,并实现输出
A_len :A数组的长度
B_len :B数组的长度
*/
void Combine_Arr(int A[],int A_len,int B[],int B_len)
{
int temp_A = A_len-1;
int temp_B = B_len-1;
int Combine_len = A_len+ B_len;
for(int i = Combine_len-1;i>=0;i--)
{
//while((temp_A>=0)&&(temp_B>=0))
if(A[temp_A]>= B[temp_B])
{
A[i] = A[temp_A--];
}
else if(A[temp_A] < B[temp_B])
{
A[i] = B[temp_B--];
}
if((temp_A<0)&&(temp_B>=0))
{
A[i] = B[temp_B--];
}
//else不用写,说明B已经都排到后面了,A在前面的就不用再动了
}
for(int i= 0;i<Combine_len;i++)
{
cout<<A[i]<<endl;
}
}
int main()
{
int M[10] = {1,2,4,100};
int N[4] = {2,6,9};
Combine_Arr(M,4,N,3);
}
A,B为两个数组,且都是按从大到小的顺序排列的现在要将B合并到A里面 并且新数组A仍然按照顺序排列,并实现输出
最新推荐文章于 2023-12-14 20:35:34 发布