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

1 4 1 2 3 1 0

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”,这个题目我相信你会很快很轻松的做出来。加油哦!

Append Code

代码

#include <iostream>
#include <iterator>
#include <set>
#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 a,b;
        cin>>a;
        for(int j=0;j<a;j++)
        {
            int t;
            cin>>t;
            A.insert(t);
        }
        cin>>b;
        for(int j=0;j<b;j++)
        {
            int t;
            cin>>t;
            B.insert(t);
        }
       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;

}

Problem B: STL——集合运算的更多相关文章

  1. STL中的set集合容器进行集合运算:并、交、差实例

    集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...

  2. [Swust OJ 632]--集合运算(set容器)

    题目链接:http://acm.swust.edu.cn/problem/632/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  3. Problem I: STL——多重集的插入和删除

    Problem I: STL--多重集的插入和删除 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1729  Solved: 1258[Submit][ ...

  4. 【Java EE 学习 28 上】【oracle学习第二天】【子查询】【集合运算】【几种数据库对象】

    一.子查询 1.为什么要使用子查询:问题不能一步求解或者一个查询不能通过一步查询得到. 2.分类:单行子查询和多行子查询. 3.子查询的本质:一个查询中包含了另外一个或者多个查询. 4.使用子查询的规 ...

  5. 详解SQL集合运算

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 ...

  6. SQL集合运算参考及案例(一):列值分组累计求和

    概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...

  7. Oracle学习之集合运算

    一.集合运算操作符  UNION:(并集)返回两个集合去掉重复值的所有的记录  UNION ALL:(并集)返回两个集合去掉重复值的所有的记录 INTERSECT:(交集)返回两个集合的所有记录,重复 ...

  8. Oracle学习(七):集合运算

    1.知识点:能够对比以下的录屏进行阅读 SQL> -- 查询10和20号部门的员工的3种方法 SQL> --1. select * from emp where deptno in (10 ...

  9. sql的集合运算

    表的加减法 union:使用union 对表进行假发(并集)运算, union等集合运算符通常都会去除重复记录. select shohin_id, shohin_mei from shohin un ...

随机推荐

  1. aop(execution()表达式)

    execution(* com.tp.soft.service..*.*(..)) 整个表达式可以分为五个部分: 1.execution(): 表达式主体. 2.第一个*号:表示返回类型,*号表示所有 ...

  2. Oracle创建表、修改字段类型

    1.创建表 1.创建表 create table SCM_PER( --SCM_PER表名 ID ) primary key,--主键ID USERID ),--用户ID --Permission v ...

  3. vue history模式

    注意: 1.前端:config.js路径问题 2.后台:配置nginx

  4. Disable access to Windows Update

    Disable access to Windows Update If this policy setting is enabled, all Windows Update features are ...

  5. C++实现的一些功能代码

    将当前时间输出到txt中: 调用c++中的fstream流文件,用tm结构获取日期和时间,其在time.h中定义 用ofstream的时候,ofstream out(txtpath,ios::app) ...

  6. 【官方下载】EasyCMDB官方基础版免费下载使用!

    链接

  7. 20190412wdVBA 排版

    Sub LayoutForExamPaper() Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA.Timer Appl ...

  8. Vue 组件的使用

    1.引入组件 import Week from '../week/main.vue' export default { name: "classMain", components: ...

  9. EF中关于日期字值的处理

    一.SQL语句方式 var datefrom = DateTime.Parse(fromdate);   var dateto = DateTime.Parse(todate); var sql = ...

  10. 华为S5700设置vlan,并绑定电脑的IP地址与mac地址。

    要求是设置两个vlan,10和20.交换机下的10网段和20网段的电脑在两个vlan当中.20网段的ip地址与mac地址绑定,从而实现下面的电脑更改ip地址或者不明来源的电脑不能连接到交换机. 1.s ...