GPLT L2-005. 集合相似度【set集合】

    xiaoxiao2021-03-25  72

    题目:集合相似度

    题意:Nc:“两个集合都有的不相等整数的个数” 意思是求俩个集合的交集个数  Nt:“两个集合一共有的不相等整数的个数” 意思是求俩个集合的并集个数 ,记得去重!

    思路:开一个set集合数组,分别将每个集合加入相应的集合中,然后再将要求的集合中查找相同元素即为Nc ,而Nt为集合的长度,当前set集合加入重复元素的话不增长总元素个数,而只是在重复元素加了个count的值为1,这样就充分的利用了set集合求交集与并集,无需手动的去重了!

    参考:寒leng的锋博客

    代码:

    #include <iostream> #include <cstdio> #include <set> #include<map> using namespace std; const int maxn = 55; set<int>st[maxn]; int main() { int n,m,k,x,y,tigital; while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%d",&m); for(int j=0;j<m;j++){ scanf("%d",&tigital); st[i].insert(tigital);} } scanf("%d",&k); while(k--){ scanf("%d%d",&x,&y); int c=0,t = st[x].size() + st[y].size(); for(set<int>::iterator it=st[x].begin();it != st[x].end();it++){ if(st[y].count(*it)) c++; } printf("%.2lf%%\n",(double)c/(double)(t-c) *100.0); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-36885.html

    最新回复(0)