zoukankan      html  css  js  c++  java
  • 1063 Set Similarity (25分)

    Given two sets of integers, the similarity of the sets is defined to be /, 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 (≤) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤) and followed by M integers in the range [0]. After the input of sets, a positive integer K (≤) 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:

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

    Sample Output:

    50.0%
    33.3%

    题目分析:利用map对应存储需要的键与值 然后从键少的那个遍历 看另一个map中有没有对应的值 注
    不能通过直接访问来判断某个键值是否存在(在map<int int>的情况下 访问不存在的键 会生成相应的键值对 其值默认为0)要通过map中find函数来判断键值是否存在
     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <climits>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<map>
     7 #include<set>
     8 #include<stack>
     9 #include<algorithm>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 map<int, int> M[50];
    14 int Size[50];
    15 int main()
    16 {
    17     int N;
    18     cin >> N;
    19     for (int i = 0; i < N; i++)
    20     {
    21         int K;
    22         cin >> K;
    23         for (int j = 0; j <K; j++)
    24         {
    25             int num;
    26             cin >> num;
    27             M[i][num]++;
    28             Size[i]++;
    29         }
    30     }
    31     int K;
    32     cin >> K;
    33     for (int i = 0; i < K; i++)
    34     {
    35         int v1, v2;
    36         int trueHave = 0;  //相等元素的个数
    37         cin >> v1 >> v2;    
    38         v1--;
    39         v2--;
    40         int size = M[v1].size() + M[v2].size();
    41         int v = (Size[v1] < Size[v2]) ? v1 : v2;
    42         int d = (Size[v1] < Size[v2]) ? v2 : v1;
    43         for (auto it : M[v])
    44             if (M[d].find(it.first)!=M[d].end())
    45                 trueHave++;
    46         printf("%.1f%%
    ", (1.0 * trueHave) / (1.0 * (size-trueHave))*100.0);
    47     }
    48 }
    View Code


  • 相关阅读:
    织梦分页条添加省略号(支持动态静态)
    织梦点击数或者其他数值过【千】过【万】过【亿】的写法
    织梦文章页每个TAG标签单独输出相关文章
    织梦验证码不显示解决方法总结
    织梦搜索结果根据搜索不同栏目显示不同搜索结果模板
    织梦正则提取中英混合字符串中第一个中文汉字
    织梦让内容摘要多行文本支持换行
    织梦dede:tag标签输入添加自增autoindex
    织梦去除底部版权power by dedecms
    织梦自定义表单添加访客提交时间和访客IP+限制每天每个IP提交表单次数
  • 原文地址:https://www.cnblogs.com/57one/p/12061617.html
Copyright © 2011-2022 走看看