Problem B: STL——集合运算
来源:互联网 发布:淘宝宝贝一键上传 编辑:程序博客网 时间:2024/06/07 06:16
Problem B: STL——集合运算
Description
集合的运算就是用给定的集合去指定新的集合。设A和B是集合,则它们的并差交补集分别定义如下:
A∪B={x|x∈A∨x∈B}
A∩B={x|x∈A∧x∈B}
A-B={x|x∈A∧x不属于 B}
SA ={x|x∈(A∪B)∧x 不属于A}
SB ={x|x∈(A∪B)∧x 不属于B}
Input
第一行输入一个正整数T,表示总共有T组测试数据。(T<=200)
然后下面有2T行,每一行都有n+1个数字,其中第一个数字是n(0<=n<=100),表示该行后面还有n个数字输入。
Output
对于每组测试数据,首先输出测试数据序号,”Case #.NO”,
接下来输出共7行,每行都是一个集合,
前2行分别输出集合A、B,接下5行来分别输出集合A、B的并(A u B)、交(A n B)、差(A – B)、补。
集合中的元素用“{}”扩起来,且元素之间用“, ”隔开。
#include <iostream>#include <set>#include <algorithm>#include <cstdio>using namespace std;int main(){ set<int> s1, s2, s3, s4, s5, s6, s7; set<int>::iterator it; int t, n, temp, cnt = 0; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> temp; s1.insert(temp); } cin >> n; for (int i = 0; i < n; i++) { cin >> temp; s2.insert(temp); } cout << "Case# " << ++cnt << ":" << endl; cout << "A = {"; int flag = 0; for (it = s1.begin(); it != s1.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; cout << "B = {"; flag = 0; for (it = s2.begin(); it != s2.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3, s3.begin())); cout << "A u B = {"; flag = 0; for (it = s3.begin(); it != s3.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s4, s4.begin())); cout << "A n B = {"; flag = 0; for (it = s4.begin(); it != s4.end(); it++){ if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s5, s5.begin())); cout << "A - B = {"; flag = 0; for (it = s5.begin(); it != s5.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; set_difference(s3.begin(), s3.end(), s1.begin(), s1.end(), inserter(s6, s6.begin())); cout << "SA = {"; flag = 0; for (it = s6.begin(); it != s6.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; set_difference(s3.begin(), s3.end(), s2.begin(), s2.end(), inserter(s7, s7.begin())); cout << "SB = {"; flag = 0; for (it = s7.begin(); it != s7.end(); it++) { if (flag++) cout << ", "; cout << *it; } cout << "}"; cout << endl; s1.clear(); s2.clear(); s3.clear(); s4.clear(); s5.clear(); s6.clear(); s7.clear(); } return 0;}
阅读全文
1 0
- Problem B: STL——集合运算
- Problem F: STL——集合运算
- Problem F: STL——集合运算
- Problem : STL——集合运算
- Problem B: STL——管道一
- Problem B: STL——管道一
- Problem B: STL——管道一
- 大数运算,HDNOJ——1002 A + B Problem II
- STL集合运算1
- STL---集合的运算
- Problem B: STL——多重集的插入和删除
- Problem B: STL——多重集的插入和删除
- Problem B: STL——多重集的插入和删除
- Problem : STL——字典
- Problem : STL——水果店
- STL实现集合交集运算
- OJ第三批——Problem O:B C++时间类的运算符重载
- 第十七周oj刷题——Problem O: B C++时间类的运算符重载
- ios RGBA数据转UIImage
- AC自动机
- express+webpack+bootstrap项目搭建从0做起
- 获取Request请求的路径信息
- org.apache.ibatis.executor.ExecutorException: Cannot get Configuration as configuration factory was
- Problem B: STL——集合运算
- 编写程序读入一组string类型的数据,并将它们存储在vector中,再将vector对象复制给一个字符指针数组。
- nodejs(一)
- this踩坑
- shiro什么时候进入doGetAuthorizationInfo(PrincipalCollection principals)
- 运动鞋
- java修饰符
- JS中的JSON对象
- 287. Find the Duplicate Number