栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【PAT】A1063 Set Similarity【Set的使用】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PAT】A1063 Set Similarity【Set的使用】

Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ 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 (≤10​4​​) and followed by M integers in the range [0,10​^9​​]. 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:


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%

给定两组整数,这些集合的相似性定义为N​C​​/N​T​​×100%,其中N​C​​ 是两个集合共享的不同公共数的数目,N​T​​ 是两组中不同数字的总数。您的工作是计算任何给定集合对的相似性。

输入规格:

每个输入文件包含一个测试用例。每种情况首先给出一个正整数N(≤50)这是总套数。然后N行跟随,每行给出一个正M的集合(≤10​4.​​) 后跟[0,10]范围内的M个整数​^9​​]. 输入集合后,一个正整数K(≤2000),然后是K行查询。每个查询提供一对集合编号(集合编号范围为1到N)。一行中的所有数字都用空格分隔。

输出规格:

对于每个查询,在一行中打印集合的相似性,以百分比形式精确到小数点后1位。 

思路分析:

用vector 存储 多个set,若求 a,b集合的相似性,遍历a的每个元素,在b中查找,存在为即为公共元素,不存在即为不同元素。


#include 
#include 
#include 
using  namespace  std;

int main(){
    int n,m,m1,k,a,b;
    cin>> n;  //输入集合的个数
    vector> v(n);   // 创建动态数组
    for(int i=0;i>m;              // 输入每个集合元素的个数
        set s;
        for(int j=0;j>m1;
            s.insert(m1);
        }
        v[i]=s;
    }
    cin >> k;   //输入查询的次数
    for(int i=0;i> a>>b; // 输入需要查询的两个集合
        int nc=0,nt=v[b-1].size();   // nc为公共元素的个数,nt两个集合不同元素的个数,初始值为b集合元素的个数
        for(auto it= v[a-1].begin();it!=v[a-1].end();it++){  // 遍历a的每个元素,在b中找,若在b中存在则nc+1,找不到则nt+1
            if(v[b-1].find(*it) ==v[b-1].end()){
                nt++;
            }
            else{
                nc++;
            }
        }
        double result;    //  相似度
        result = nc*1.0/nt *100;
        printf("%.1fn",result);

    }
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/716930.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号