Problem F: STL——集合运算
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 3767 Solved: 1927
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”,这个题目我相信你会很快很轻松的做出来。加油哦!
Append Code
한국어< 中文 فارسی English ไทย
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin:admin
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
int T, j, n, a[110];
cin >> T;
for(int i = 1; i <= T; i++)
{
set <int> A; set <int> B; set <int> C;
A.clear(); B.clear(); C.clear(); set <int>:: iterator set_iter;
cin >> n;
for( j = 0; j < n; ++j)
cin >> a[j];
A.insert(a, a+n);
cin >> n;
for(int j = 0; j < n; j++)
cin >> a[j];
B.insert(a, a + n);
cout << "Case# " << i << ":" << endl;
cout << "A = {";
for(set_iter = A.begin(); set_iter != A.end() ; ++set_iter) {
if(set_iter != A.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" <<endl;
cout << "B = {";
for(set_iter = B.begin(); set_iter != B.end() ; ++set_iter) {
if(set_iter != B.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" <<endl;
cout << "A u B = {";
set_union(A.begin(), A.end(), B.begin(), B.end(), insert_iterator< set<int > >(C,C.begin()));
for(set_iter = C.begin(); set_iter != C.end() ; ++set_iter) {
if(set_iter != C.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" <<endl;
C.clear();
set_intersection( A.begin(), A.end(), B.begin(), B.end(), insert_iterator< set<int> >(C, C.begin()));
cout << "A n B = {";
for(set_iter = C.begin(); set_iter != C.end() ; ++set_iter) {
if(set_iter != C.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" <<endl;
C.clear();
cout << "A - B = {";
set_difference(A.begin(), A.end(), B.begin(), B.end(), insert_iterator< set<int> >(C, C.begin()));
for(set_iter = C.begin(); set_iter != C.end(); set_iter++ )
{
if(set_iter != C.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" << endl;
C.clear(); set<int> C2;
cout << "SA = {";
set_union(A.begin(),A.end(),B.begin(),B.end(),insert_iterator<set<int> >(C2,C2.begin()));
set_difference(C2.begin(), C2.end(), A.begin(), A.end(), insert_iterator< set<int> >(C, C.begin()));
for(set_iter = C.begin(); set_iter != C.end(); set_iter++ )
{
if(set_iter != C.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" << endl;
C.clear();
cout << "SB = {";
set_difference(C2.begin(), C2.end(), B.begin(), B.end(), insert_iterator< set<int> >(C, C.begin()));
for(set_iter = C.begin(); set_iter != C.end(); set_iter++ )
{
if(set_iter != C.begin())
cout << ", ";
cout << *set_iter;
}
cout << "}" << endl;
A.clear(); B.clear(); C.clear(); C2.clear();
}
}