STL -- set operation

Problem F: STL -- set operation time limit: 1 sec memory limit: 128 MB
Submit: 6392 Solved: 3017
The operation of the [Submit][Status]Description set is to specify A new set with A given set. If A and B are sets, their dyadic complementary sets are defined as follows:
A∪B={x|x∈A∨x∈B}
A∩B={x|x∈A∧x∈B}
A-B={x|x ∈ a Λ X does not belong to B}
SA ={x|x ∈ (A ∪ B) ∧ X does not belong to A}
SB ={x|x ∈ (A ∪ B) ∪ X does not belong to B}

Input the first line input a positive integer T, indicating that there are a total of T groups of test data. (T<=200)
Then there are 2T lines below, each line has n+1 numbers, the first of which is n (0 < = n < = 100), indicating that there are n number inputs after the line.
Output for each group of test data, first output the test data serial number, "case ා. No",
The next output consists of 7 lines, each of which is a set.
The first two lines output sets A and B respectively, and the next five lines output the Union (A u B), intersection (A n B), difference (A – B) and complement of sets A and B respectively.
The elements in the collection are expanded with "{}" and separated with ",".
Sample Input
1
4 1 2 3 1
0
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 if you will use Baidu search keyword "stl set", this topic I believe you will soon be very easy to do. Come on!

Append Code

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

Posted by Zan on Sat, 02 Nov 2019 03:06:46 -0700