[蓝桥杯2017初赛]兴趣小组

题目描述

为丰富同学们的业余文化生活,某高校学生会创办了3个兴趣小组(以下称A组,B组,C组)。
每个小组的学生名单分别在【A.txt】,【B.txt】和【C.txt】中。
每个文件中存储的是学生的学号。
由于工作需要,我们现在想知道:
既参加了A组,又参加了B组,但是没有参加C组的同学一共有多少人?

输入

输出

输出一个整数表示答案
 
 

答案:20

题解:先分别数出A、B、C各有多少个数据(A=150,B=C=250);用map<string ,int>建立映射判断即可

#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#define ll long long
using namespace  std;
map<string,int>A;
map<string,int>B;
map<string,int>C;
int main()
{
    int ans=0,a=150,b=250,c=250;
    string s;
    while(a--)
    {
        cin>>s;
        A[s]=1;
    }
    while(b--)
    {
        cin>>s;
        B[s]=1;
    }
    while(c--)
    {
        cin>>s;
        C[s]=1;
    }
    ll n=max(A.size(),max(B.size(),C.size()));
    map<string,int>::iterator it;
    for(it=A.begin();it!=A.end();it++)
        if(B.count(it->first)&&C.count(it->first)==0)
            ans++;
    cout<<ans<<endl;
    //cout<<20<<endl;
    return 0;

}
原文地址:https://www.cnblogs.com/-citywall123/p/12339838.html