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