生活随笔
收集整理的這篇文章主要介紹了
set的相关习题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題 A: Set Similarity
http://codeup.cn/problem.php?cid=100000597&pid=0
題目說的是:先計算兩個集合去重后的交集Nc,然后計算兩個元素并集并去重Nt,兩者相除。
例:
1 和
2 集合 交集有
87 101
并集有
99 87 101 5
2/4=0.51 和
3 集合 交集有
99 101
并集有
87 101 99 18 5 135
2/6=1/3
思路就是求出 a 和 b 相重復的數的個數n
再用 a.size()+b.size()-n=并集的個數m
n/m
#include<cstdio>
#include<set>
#include<iostream>
using namespace std
;
set
<int> s
[200];
int main(void)
{int n
;int m
;int i
,j
;int number
;cin
>>n
;j
=0;while(n
){cin
>>m
;for(i
=0;i
<m
;i
++){scanf("%d",&number
);s
[j
].insert(number
);}n
--;j
++; }int w
,a
,b
;set
<int>::iterator it
;cin
>>w
;while(w
){scanf("%d %d",&a
,&b
);int temp
;int count
=0;for(it
=s
[a
-1].begin();it
!=s
[a
-1].end();it
++){if(s
[b
-1].find(*it
)!=s
[b
-1].end()){count
++;}}int ans
=s
[b
-1].size()+s
[a
-1].size()-count
;printf("%.1lf%%\n",100*(double)count
/ans
);w
--;}return 0;
}
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的set的相关习题的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。