Description

给定两个集合A和B的所有元素,计算它们的交、并、差集。

Input

输入数据有多组,第一行为数据的组数T,接下来有2T行,每组数据占2行,每行有若干个整数,第一行的所有整数构成集合A,第二行的所有整数构成集合B,分别用空格分隔。A和B最多分别不超过100个元素。

Output

输出A、B的交、并、差集中的所有元素(以递增顺序)。每个集合占一行,数据元素之间用空格分隔。

Sample Input 

1
0 1 2 3 4 5 6 7 8 8
3 6 8 9

Sample Output

3 6 8
0 1 2 3 4 5 6 7 8 9
0 1 2 4 5 7

#include<set>
#include<algorithm>
#include<iterator>
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
set<int>v1,v2,v3;
string s1,s2;
int i,j,t,a;
set<int>::iterator it;
cin>>t;
getchar();
while(t--)
{
j=;
getline(cin,s1); //数字类型的字符串导入s1
stringstream ss1(s1); //字符串s1再导入ss1流中
while(ss1>>a) //流中的字符串s1导入int a中变成数字
{
v1.insert(a); //将a中暂时保存的数字转入set
}
getline(cin,s2);
stringstream ss2(s2);
while(ss2>>a)
{
v2.insert(a);
}
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),inserter(v3,v3.begin()));
//交
for(it=v3.begin();it!=v3.end();it++)
{
if(j==)
{
cout<<*it;
j=;
}
else cout<<" "<<*it;
}
cout<<endl;
v3.clear(); //清空v3集合
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),inserter(v3,v3.begin()));
//并
j=;
for(it=v3.begin();it!=v3.end();it++)
{
if(j==)
{
cout<<*it;
j=;
}
else cout<<" "<<*it;
}
cout<<endl;
v3.clear();
set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),inserter(v3,v3.begin()));
//差
j=;
for(it=v3.begin();it!=v3.end();it++)
{
if(j==)
{
cout<<*it;
j=;
}
else cout<<" "<<*it;
}
cout<<endl;
v3.clear();
v1.clear(); //清空v1集合
v2.clear(); //清空v2集合
}
return ;
}

最新文章

  1. 【BZOJ】3996: [TJOI2015]线性代数
  2. elasticsearch 之mapping
  3. sql存储过程中加引号
  4. Sudoku 数独游戏
  5. 深入理解OOP(四): 多态和继承(抽象类)
  6. Elsevier 投稿各种状态总结
  7. 第六十八篇、OC_按照某一字段对数值进行排序
  8. python re 正则表达式[转]
  9. 2016022601 - redis入门了解
  10. ♫【jQuery】detach
  11. C#操作注册表——读、写、删除、判断等基本操作
  12. MySQL开启远程连接权限
  13. Linux(Ubuntu)使用日记(七)------终端控制器Terminator安装使用
  14. linux为什么要使用CentOS开发?
  15. Linux7系列阅读
  16. 应用间共享文件 FileProvider
  17. 通过 Ansible 创建 Jenkins Server
  18. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
  19. PHP 支持 JQuery 的 JSONP 跨域访问
  20. LeetCode--110--平衡二叉树

热门文章

  1. 融云会话界面导航上移-使用IQKeyboardManager
  2. HttpClient请求工具类
  3. ThreadPoolExecutor(下篇)
  4. c#winform循环播放多个视频
  5. PAT 1051 Pop Sequence
  6. java常用API之字符串缓冲区
  7. 函数进阶3 —— 生成器、yield from
  8. Bootstrap导航栏navbar源码分析
  9. 打杂程序员之nginx服务配置
  10. spring mvc&amp;mybatis搭配使用心得