给你两个集合,要求{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
/*刚开始看到题就知道是用set容器,但是中间的set<int>::iterator it;这个格式写错了导致没写出来,然后换一种最基础的思路,数组 ,其实这种方法对时间跟内存要求太高了 最后果然Memory Limit Exceeded*/
//Memory Limit Exceeded代码
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int b[100010005];
int a[100000000];
int main()
{
int n,m,i,j,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(b,0,sizeof(b));
t=0;
for(i = 0 ;i < n;i++)
scanf("%d",&a[i]);
for(j = 0; j < m;j++)
scanf("%d",&b[j]);
for(i = 0;i < n;i++)
{
t=0;
for(j = 0 ;j < m;j++)
{
if(a[i]==b[j])
break;
else
{
t++;
}
}
if(t==m)
{
b[m+i]=a[i];
}
}
sort(b,b+100000);
for(i = 0;i < 100000;i++)
if(b[i]==0)
continue;
else printf("%d ",b[i]);
printf("\n");
}
}
//set AC代码
#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
int main()
{
set<int> s;
int n,m,i,j;
while(cin>>n>>m)
{
for(i = 0;i < n+m;i++)
{
cin>>j;
s.insert(j);
}
set<int>::iterator it=s.begin();
cout<<*it;
it++;
while(it!=s.end())
{
cout<<" "<<*it;
it++;
}
printf("\n");
s.clear();
}
return 0;
}
最后还是感慨下,多刷题是有它的好处的。哎,只能怪自己做题太少,加油!