{A} + {B}
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22060 Accepted Submission(s): 9094
Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.
注:同一个集合中不会有两个相同的元素.
Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.
Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.
Sample Input
1 2 1 2 3 1 2 1 1 2
Sample Output
1 2 3 1 2
Author
xhd
Source
Recommend
使用c++ STL中的内置函数sort就可以nlogn的效率排好序
输出过程中使用一个变量tmp,记录前一个输出的值,如果当前准备输出的值相同则跳过。
代码如下:
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2e4+20;
int a[maxn],n,m,tot;
int main(){
while (cin >> n >> m ){
tot=0;
for (int i=0;i<n+m;i++) cin >> a[tot++];
sort(a,a+tot);
cout<<a[0];
int tmp=a[0];
for (int i=1;i<tot;i++) {
if (tmp!=a[i]) {
cout<<" "<<a[i];
tmp=a[i];
}
}
cout<<endl;
}
return 0;
}