Problem B: STL——集合运算
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 #include #include #include using namespace std;int main(){ set s1, s2, s3, s4, s5, s6, s7; set::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;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~