1063. Set Similarity (25)

绝地灬酷狼 2022-05-30 00:09 241阅读 0赞

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the number of distinct common numbers shared by the two sets, and Nt is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (<=50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

  1. 3
  2. 3 99 87 101
  3. 4 87 101 5 87
  4. 7 99 101 18 5 135 18 99
  5. 2
  6. 1 2
  7. 1 3

Sample Output:

  1. 50.0%
  2. 33.3%

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<map>
  5. using namespace std;
  6. map<int,int> Map[51];
  7. int main()
  8. {
  9. int i,j,n,m,k,t,set1,set2,cnum,total,l1,l2;
  10. scanf("%d",&n);
  11. for(i=1;i<=n;i++)
  12. {
  13. scanf("%d",&m);
  14. for(j=0;j<m;j++)
  15. {
  16. scanf("%d",&k);
  17. Map[i][k]=1;
  18. }
  19. }
  20. scanf("%d",&t);
  21. for(i=0;i<t;i++)
  22. {
  23. total=0;
  24. scanf("%d %d",&set1,&set2);
  25. map<int,int>::iterator it;
  26. for(it=Map[set1].begin();it!=Map[set1].end();it++)
  27. {
  28. if(Map[set2].find(it->first)!=Map[set2].end())
  29. total++;
  30. }
  31. printf("%.1lf%%\n",1.0*total/(Map[set1].size()+Map[set2].size()-total)*100);
  32. }
  33. return 0;
  34. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT A1063

    ![clipboard.png][] 这里面还是用到了set去重,还是要多掌握stl的用法; 这里注意一个巧妙地处理; 由于我们寻找的时两个集合a,b的不重复元素,

    相关 PAT A1063

    ![clipboard.png][] 这里面还是用到了set去重,还是要多掌握stl的用法; 这里注意一个巧妙地处理; 由于我们寻找的时两个集合a,b的不重复元素,

    相关 P1063 能量项链

    题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链。在项链上有N颗能量眼珠。能量眼珠是一颗有头标记与尾标记的眼珠子,这些标记对应着某个正整数。并且,对于相