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;}