c+合并两个数组_C / C ++程序,用于两个数组的并集

c+合并两个数组

Here you will get C/C++ program to find union of two arrays.

在这里,您将获得C / C ++程序来查找两个数组的并集。

For example:

例如:

First array: {1, 3, 7, 9}

第一个数组:{1、3、7、9}

Second array: {1, 4, 6}

第二数组:{1、4、6}

Union: {1, 3, 4, 7, 6, 9}

联合:{1、3、4、7、6、9}

C / C ++程序,用于两个数组的并集 (C/C++ Program for Union of Two Arrays)

两个排序数组的并集 (Union of Two Sorted Arrays)

If two arrays are sorted then their union can be found in following way.

如果对两个数组进行了排序,则可以通过以下方式找到它们的并集。

C Program

C程序

#include<stdio.h>
 
int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m;
	
	printf("Enter size of first array:");
	scanf("%d",&n);
	printf("Enter elements of first array in ascending order:\n");
	for(i=0;i<n;++i){
		scanf("%d",&a1[i]);
	}
	
	printf("\nEnter size of second array:");
	scanf("%d",&m);
	printf("Enter elements of second array in ascending order:\n");
	for(i=0;i<m;++i){
		scanf("%d",&a2[i]);
	}
	
	for(i=0,j=0,k=0;i<n&&j<m;){
		if(a1[i]<a2[j]){
			u[k]=a1[i];
			i++;
			k++;
		}
		else if(a1[i]>a2[j]){
			u[k]=a2[j];
			j++;
			k++;
		}
		else{
			u[k]=a1[i];
			i++;
			j++;
			k++;
		}
	}
	
	if(i<n){
		for(;i<n;++i){
			u[k]=a1[i];
			k++;
		}
	}
	else if(j<m){
		for(;j<m;++j){
			u[k]=a2[j];
			k++;
		}
	}
	
	printf("\nUnion of two arrays is:\n");
	for(i=0;i<k;++i){
		printf("%d ",u[i]);
	}
 
    return 0;
}

Output

输出量

Enter size of first array:4 Enter elements of first array in ascending order: 1 2 3 5

输入第一个数组的大小:4 以升序输入第一个数组的元素: 1 2 3 5

Enter size of second array:5 Enter elements of second array in ascending order: 1 3 5 7 9

输入第二个数组的大小:5 以升序输入第二个数组的元素: 1 3 5 7 9

Union of two arrays is: 1 2 3 5 7 9

两个数组的并集是: 1 2 3 5 7 9

C++ Program

C ++程序

#include<iostream>
 
using namespace std;
 
int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m;
	
	cout<<"Enter size of first array:";
	cin>>n;
	cout<<"Enter elements of first array in ascending order:\n";
	for(i=0;i<n;++i){
		cin>>a1[i];
	}
	
	cout<<"\nEnter size of second array:";
	cin>>m;
	cout<<"Enter elements of second array in ascending order:\n";
	for(i=0;i<m;++i){
		cin>>a2[i];
	}
	
	for(i=0,j=0,k=0;i<n&&j<m;){
		if(a1[i]<a2[j]){
			u[k]=a1[i];
			i++;
			k++;
		}
		else if(a1[i]>a2[j]){
			u[k]=a2[j];
			j++;
			k++;
		}
		else{
			u[k]=a1[i];
			i++;
			j++;
			k++;
		}
	}
	
	if(i<n){
		for(;i<n;++i){
			u[k]=a1[i];
			k++;
		}
	}
	else if(j<m){
		for(;j<m;++j){
			u[k]=a2[j];
			k++;
		}
	}
	
	cout<<"\nUnion of two arrays is:\n";
	for(i=0;i<k;++i){
		cout<<u[i]<<" ";
	}
 
    return 0;
}

两个未排序数组的并集 (Union of Two Unsorted Arrays)

If two arrays are unsorted then their union can be found in following way.

如果两个数组未排序,则可以按以下方式找到它们的并集。

C Program

C程序

#include<stdio.h>
 
int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m,flag;
	
	printf("Enter size of first array:");
	scanf("%d",&n);
	printf("Enter elements of first array:\n");
	for(i=0;i<n;++i){
		scanf("%d",&a1[i]);
	}
	
	printf("\nEnter size of second array:");
	scanf("%d",&m);
	printf("Enter elements of second array:\n");
	for(i=0;i<m;++i){
		scanf("%d",&a2[i]);
	}
	
	k=0;
	for(i=0;i<n;++i){
		u[k]=a1[i];
		k++;
	}
	
	for(i=0;i<m;++i){
		flag=1;
		for(j=0;j<n;++j){
			if(a2[i]==a1[j]){
				flag=0;
				break;
			}
		}
		
		if(flag){
			u[k]=a2[i];
			k++;
		}
	}
	
	printf("\nUnion of two arrays is:\n");
	for(i=0;i<k;++i){
		printf("%d ",u[i]);
	}
 
    return 0;
}

Output

输出量

Enter size of first array:3 Enter elements of first array: 1 3 5

输入第一个数组的大小:3 输入第一个数组的元素: 1 3 5

Enter size of second array:4 Enter elements of second array: 1 2 3 6

输入第二个数组的大小:4 输入第二个数组的元素: 1 2 3 6

Union of two arrays is: 1 3 5 2 6

两个数组的并集为: 1 3 5 2 6

C++ Program

C ++程序

#include<iostream>
 
using namespace std;
 
int main()
{
	int a1[20],a2[20],u[40],i,j,k,n,m,flag;
	
	cout<<"Enter size of first array:";
	cin>>n;
	cout<<"Enter elements of first array in ascending order:\n";
	for(i=0;i<n;++i){
		cin>>a1[i];
	}
	
	cout<<"\nEnter size of second array:";
	cin>>m;
	cout<<"Enter elements of second array in ascending order:\n";
	for(i=0;i<m;++i){
		cin>>a2[i];
	}
	
		k=0;
	for(i=0;i<n;++i){
		u[k]=a1[i];
		k++;
	}
	
	for(i=0;i<m;++i){
		flag=1;
		for(j=0;j<n;++j){
			if(a2[i]==a1[j]){
				flag=0;
				break;
			}
		}
		
		if(flag){
			u[k]=a2[i];
			k++;
		}
	}
 
	cout<<"\nUnion of two arrays is:\n";
	for(i=0;i<k;++i){
		cout<<u[i]<<" ";
	}
 
    return 0;
}

翻译自: https://www.thecrazyprogrammer.com/2016/08/cc-program-union-two-arrays.html

c+合并两个数组

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值