Problem F: 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)、补。
集合中的元素用“{}”扩起来,且元素之间用“, ”隔开。

Sample Input

14 1 2 3 10

Sample Output

Case# 1:A = {1, 2, 3}B = {}A u B = {1, 2, 3}A n B = {}A - B = {1, 2, 3}SA = {}SB = {1, 2, 3}

HINT

如果你会用百度搜一下关键字“stl set”,这个题目我相信你会很快很轻松的做出来。加油哦
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
void print(const set<int> &A)
{
    set<int>::iterator it;
    cout<<"{";
    for(it=A.begin();it!=A.end();it++){
        if(it==A.begin())
            cout<<*it;
        else
            cout<<", "<<*it;
    }
    cout<<"}"<<endl;
}
int main()
{
    set<int> A;
    set<int> B;
    set<int> tm1,tm2,tm3,tm4,tm5;
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cout<<"Case# "<<i+1<<":"<<endl;
        int m,k;
        cin>>m;
        for(int j=0;j<m;j++){
          int t;
          cin>>t;
          A.insert(t);
        }
        cin>>k;
        for(int j=0;j<k;j++){
          int r;
          cin>>r;
          B.insert(r);
        }
        cout<<"A = ";
        print(A);
        cout<<"B = ";
        print(B);
        set_union(A.begin(), A.end(), B.begin(), B.end(), inserter(tm1, tm1.begin()));
        cout<<"A u B = ";
        print(tm1);
        set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(tm2, tm2.begin()));
        cout<<"A n B = ";
        print(tm2);
        set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(tm3, tm3.begin()));
        cout<<"A - B = ";
        print(tm3);
        set_difference(tm1.begin(), tm1.end(), A.begin(), A.end(), inserter(tm4, tm4.begin()));
        cout<<"SA = ";
        print(tm4);
        set_difference(tm1.begin(), tm1.end(), B.begin(), B.end(), inserter(tm5, tm5.begin()));
        cout<<"SB = ";
        print(tm5);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值