A,B为两个数组,且都是按从大到小的顺序排列的现在要将B合并到A里面 并且新数组A仍然按照顺序排列,并实现输出

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值