题目链接:http://acm.swust.edu.cn/problem/632/

Time limit(ms): 1000      Memory limit(kb): 65535
 
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
输入可能有2~4行数据 
第一行输入集合A的元素个数M1(M1>=0),接下来一行输入集合A的元素 
第三行输入集合B的元素个数M2(M2>=0),最后一行输入集合B的元素

 
Output
输出共7行 
前2行分别输出集合A、B,接下5行来分别输出集合A、B的并(A∪B)、交(A∩B)、差(A – B)、补。

 
Sample Input
4
1 3 2 1
0
Sample Output
A={1, 2, 3}
B={}
AuB={1, 2, 3}
AnB={}
A-B={1, 2, 3}
SA={}
SB={1, 2, 3}
Hint
为了唯一确定输出结果,集合的元素按升序输出
 
解题思路:注意到了升序输出,结合集合运算的特点,直接利用set容器水过Orz~~~
 #include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std; set<int>a, b, c, d, e, f, g;
int n, m, x; void init(){
a.clear(); b.clear();
c.clear(); d.clear();
e.clear(); f.clear(); g.clear();
} void Order(string str, set<int>s){
cout << str << "={";
for (set<int>::iterator it = s.begin(); it != s.end(); it++){
if (it == s.begin()) cout << *it;
else cout << ", " << *it;
}
cout << "}" << endl;
}
int main(){
while (cin >> n){
init();
for (int i = ; i < n; i++){
cin >> x;
a.insert(x);
}
cin >> m;
for (int i = ; i < m; i++){
cin >> x;
b.insert(x);
}
set_union(a.begin(), a.end(), b.begin(), b.end(), insert_iterator<set<int> >(c, c.begin()));//并
set_intersection(a.begin(), a.end(), b.begin(), b.end(), insert_iterator<set<int> >(d, d.begin()));//交
set_difference(a.begin(), a.end(), b.begin(), b.end(), insert_iterator<set<int> >(e, e.begin()));//差
set_difference(c.begin(), c.end(), a.begin(), a.end(), insert_iterator<set<int> >(f, f.begin()));
set_difference(c.begin(), c.end(), b.begin(), b.end(), insert_iterator<set<int> >(g, g.begin()));
Order("A", a);
Order("B", b);
Order("AuB", c);
Order("AnB", d);
Order("A-B", e);
Order("SA", f);
Order("SB", g);
}
return ;
}

[Swust OJ 632]--集合运算(set容器)的更多相关文章

  1. [Swust OJ 403]--集合删数

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

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

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

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

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

  4. 详解SQL集合运算

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

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

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

  6. Oracle学习之集合运算

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

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

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

  8. sql的集合运算

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

  9. [Swust OJ 404]--最小代价树(动态规划)

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

随机推荐

  1. tomcat oracle 连接池配置

    <?xml version='1.0' encoding='utf-8'?> <Context displayName="zcgl" docBase=" ...

  2. Count the Colors(线段树,找颜色段条数)

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  3. CSS3 grayscale滤镜图片变黑白实例页面

    在网站加入友情链接的LOGO时,因为不同logo颜色的问题,和主题色调可能产生冲突, 我选择用CSS3滤镜让logo变黑白,hover时变回原本的彩色 CSS代码: .gray { -webkit-f ...

  4. 【转】CoreData以及MagicalRecord (一)

    先粗略的了解下CoreData中的一些核心概念 1. CoreData 的核心概念 先上两幅关键的概念图 (1)NSManagedObjectModel 托管对象模型(MOM)是描述应用程序的数据模型 ...

  5. Spring MVC返回的json如何去除根节点名称

    spring xml中配置视图如果是如下 <property name="defaultViews"> <list> <bean class=&quo ...

  6. 将窗口置顶的方法:SetWindowPos、AttachThreadInput、SwitchToThisWindow

    将窗口置顶的方法:SetWindowPos.AttachThreadInput.SwitchToThisWindow [转]http://hi.baidu.com/neil_danky/item/f9 ...

  7. apache hide index.php

    <Directory "D:/usr/local/www">    AllowOverride all    Options +FollowSymLinks +SymL ...

  8. 使用jquery控制display属性

    //隐藏 $("#id").css('display','none'); //显示 $("#id").css('display','block'); 或 $(& ...

  9. Android 汉字转拼音之工具篇

    /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  10. 0617 python 基础04

    控制流--for 循环 >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 换行输出 >>> for i in range(1 ...