题目描述
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
输入
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
输出
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
样例输入 Copy
1 2 1 2 3 1 2 1 1 2
样例输出 Copy
1 2 3 1 2
#include<iostream>
#include<set>
using namespace std;
int main()
{
int A_size, B_size;
set<int> A_and_B;
while (cin >> A_size >> B_size) {
//插入A集合中的元素
while (A_size--) {
int a;
cin >> a;
A_and_B.insert(a);
}
//插入B集合中的元素
while (B_size--) {
int b;
cin >> b;
A_and_B.insert(b);
}
set<int>::iterator it;
int cnt = 0;
for (it = A_and_B.begin(); it != A_and_B.end(); it++) {
cnt++;
if (cnt == A_and_B.size()) {
cout << *it;
break;
}
else {
cout << *it << " ";
}
}
cout << endl;
A_and_B.clear();
}
}