天天看点

求两个数组的并集

#include"iostream"
using namespace std;
int main()
{
	int a[100], b[100];
	int m, n, i, j,k=0;
	cout << "a表中有多少数" << endl;
	cin >> n;
	cout << "具体数" << endl;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << "b表中有多少数" << endl;
	cin >> m;
	cout << "具体数" << endl;
	for (i = 0; i < m; i++)
	{
		cin >> b[i];
	}
	for (j = 0; j < m; j++)
	{
		i = 0;
		while (b[j] != a[i])
		{
			i++;
		}
		if (i>n+k-1)
		{
			a[n + k] = b[j];
			k++;
		}
	}
	for (i = 0; i < n+k; i++)
		cout << a[i] << " ";
	cout << endl;
	system("pause");
	return 0;
}