Problem B: STL——集合运算
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2547 Solved: 1329
[ Submit][ Status][ Web Board]
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 <algorithm> #include <set> using namespace std; void print(const set<int> &t) { set<int>::iterator it; cout << "{"; for(it = t.begin(); it != t.end(); it++) if(it == t.begin()) cout << *it; else cout << ", " << *it; cout << "}" << endl; } int main() { int t; cin>>t; int i=0; while(t--) { cout<<"Case# "<<++i<<":"<<endl; set<int>s1; set<int>s2; set<int>tmp1,tmp2,tmp3,tmp4,tmp5; int m; cin>>m; for(int i=0;i<m;i++) { int tmp; cin>>tmp; s1.insert(tmp); } int k; cin>>k; for(int i=0;i<k;i++) { int tmp; cin>>tmp; s2.insert(tmp); } cout<<"A = "; print(s1); cout<<"B = "; print(s2); set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(tmp1,tmp1.begin())); cout<<"A u B = "; print(tmp1); set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmp2, tmp2.begin())); cout<<"A n B = "; print(tmp2); set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmp3, tmp3.begin())); cout<<"A - B = "; print(tmp3); set_difference(tmp1.begin(), tmp1.end(), s1.begin(), s1.end(), inserter(tmp4, tmp4.begin())); cout<<"SA = "; print(tmp4); set_difference(tmp1.begin(), tmp1.end(), s2.begin(), s2.end(), inserter(tmp5, tmp5.begin()));///set_??(a.begin(),a.end(),b.begin(),b.end(),inserter(x,x.begin()); cout<<"SB = "; print(tmp5); } return 0; } /* #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstring> using namespace std; const int maxn = 40000 + 10; int a[maxn]; int b[maxn]; int main() { int t; scanf("%d", &t); for (int kase = 1; kase <= t; kase++) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); printf("Case #%d:\n", kase); int num1, num2; scanf("%d", &num1); for (int i = 0; i < num1; i++) scanf("%d", &a[i]); scanf("%d", &num2); for (int i = 0; i < num2; i++) scanf("%d", &b[i]); vector<int> aANDb, ab, a_b, b_a; for (int i = 0, j = 0; i < num1 || j < num2;) { if (i < num1 && j < num2 && a[i] == b[j]) { aANDb.push_back(a[i]); ab.push_back(a[i]); i++, j++; } else if (i < num1 && j < num2 && a[i] < b[j] || j >= num2) { aANDb.push_back(a[i]); a_b.push_back(a[i]); i++; } else if (i < num1 && j < num2 && b[j] < a[i] || i >= num1) { aANDb.push_back(b[j]); b_a.push_back(b[j]); j++; } } int len = ab.size(); for (int i = 0; i < len; i++) { if (i) printf(" "); printf("%d", ab[i]); } puts(""); len = aANDb.size(); for (int i = 0; i < len; i++) { if (i) printf(" "); printf("%d", aANDb[i]); } puts(""); len = a_b.size(); for (int i = 0; i < len; i++) { if (i) printf(" "); printf("%d", a_b[i]); } puts(""); len = b_a.size(); for (int i = 0; i < len; i++) { if (i) printf(" "); printf("%d", b_a[i]); } puts(""); } return 0; }*/