计蒜客 练习题:计算集合的并 set

给你两个集合,计算其并集,即 \{A\} + \{B\}{A}+{B}

注:\{A\} + \{B\}{A}+{B} 中不允许出现重复元素,但是 \{A\}{A} 与 \{B\}{B} 之间可能存在相同元素。

输入格式

输入数据分为三行,第一行有两个数字 n, m(0<n,m\leq 10000)n,m(0<n,m10000),分别表示集合 A 和集合 B 的元素个数。后两行分别表示集合 A 和集合 B。每个元素为不超出 int 范围的整数,每个元素之间用一个空格隔开。

输出格式

输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间用一个空格隔开。

样例输入1

1 2
1
2 3

样例输出1

1 2 3

样例输入2

1 2
1
1 2

样例输出2

1 2

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include<set>
using namespace std;

int main(){
  int a[10010],b[10010];
  int n ,m ;
  scanf("%d%d",&n,&m);
  set<int>arry ;
  for(int i=0;i<n;i++)
  {
      scanf("%d",&a[i]);
      arry.insert(a[i]);
  }
  for(int j=0 ; j<m ; j++)
  {
      scanf("%d",&b[j]);
      arry.insert(b[j]);
  }


 for (set<int>::iterator it = arry.begin(); it !=arry.end(); ++it)
        {
       printf("%d",*it);
       int se=*it ; //删除首元素 解决空格问题
       arry.erase(se);
       break ;


}
for (set<int>::iterator it = arry.begin(); it !=arry.end(); ++it)
        {
       printf(" %d",*it);




}


return 0 ;
}


发布了19 篇原创文章 · 获赞 8 · 访问量 4150

猜你喜欢

转载自blog.csdn.net/paohui001lqp/article/details/79440033