解题思路:
注意事项:
参考代码:
#include<iostream>
using namespace std;
typedef struct List
{
int L[200];
int length;
}List;//定义线性表结构体
void show(List L)//输出函数
{
for (int i = 0; i < L.length; i++)
cout << L.L[i] << " ";
cout << endl;
}
void Union(List &a, List &b)
{
for (int i = 0; i < b.length; i++)
{
int temp = b.L[i];
int flag = 1; //flag判断是否有重复值
for (int j = 0; j < a.length; j++)
{
if (a.L[j] == temp)
{
flag = 0;
break;//如果有重复值,flag=0;跳出循环
}
}
if (flag)//flag==1即无重复值,添加元素
{
a.L[a.length] = temp;
a.length++;
}
show(a);
}
}
int main()
{
List L1, L2;
int m, n;
while ( cin >> m)
{
for (int i = 0; i < m; i++)
{
cin >> L1.L[i];
}
L1.length = m;
cin >> n;
for (int j = 0; j < n; j++)
{
cin >> L2.L[j];
}
L2.length = n;
show(L1);
show(L2);
Union(L1, L2);
cout << endl;
}
return 0;
}
0.0分
0 人评分