{A} + {B}
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 91 Accepted Submission(s) : 41
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
Source
HDU 2006-5 Programming Contest
#include<string>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include<iomanip>
#include<memory.h>
using namespace std;
int main()
{
int a[10000], b[10000];
int n, m, i, j, t;
while(scanf("%d%d", &n, &m) != EOF) {
for(i = 0; i < n; i++) {
scanf("%d", a+i);
}
for(i = 0; i < m; i++) {
scanf("%d", b+i);
}
sort(a,a+n);
sort(b,b+m);
i = 0;
j = 0;
t = 0;
while(i < n && j < m) {
if(a[i] < b[j]) {
if(t == 0) {
printf("%d", a[i]);
t = 1;
} else {
printf(" %d", a[i]);
}
i++;
}
else if(a[i] > b[j]) {
if(t == 0) {
printf("%d", b[j]);
t = 1;
} else {
printf(" %d", b[j]);
}
j++;
} else {
if(t == 0) {
printf("%d", a[i]);
t = 1;
} else {
printf(" %d", a[i]);
}
i++;
j++;
}
}
while(i < n) {
printf(" %d", a[i]);
i++;
}
while(j < m) {
printf(" %d", b[j]);
j++;
}
printf("\n");
}
return 0;
}