【PAT】1063 Set Similarity (25分)(模拟 STL 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%

ps:英语不好真的太糟心了,基本靠输入输出猜的,一定要好好学英语啊!!

题目大意:求两个集合的相似度,相似度是由 相同元素/两个集合总元 素得到的,说白了就是交集除以并集。

解题思路:将所有集合放在一个二维数组st中(不会超过50个集合),然后用set的find方法去查找交集,使用迭代器,固定一个set,在用另一个set的元素来匹配。

 

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;

const int N=51;
set<int> st[N];
int main()
{
    int n,k,num,temp,set1,set2;
    //freopen("input.txt","r",stdin);
    scanf("%d",&n);//输入集合个数
    for(int i=1;i<=n;i++){
        scanf("%d",&k);//每个集合有多少个元素
        for(int j=0;j<k;j++){
            scanf("%d",&temp);
            st[i].insert(temp);//插入该集合中的所有元素
        }
    }
    scanf("%d",&num);//需要比较的次数
    for(int i=0;i<num;i++)
    {
        scanf("%d%d",&set1,&set2);//需要比较的集合是哪两个
       
      int totalNum=st[set2].size(),sameNum=0;//并集个数,交集个数
    	for(auto it=st[set1].begin();it!=st[set1].end();it++){//set<int>::iterator=auto
        if(st[set2].find(*it)!=st[set2].end()) sameNum++;//找到元素,交集数++
        else totalNum++;//没找到元素,总数++
    }
    printf("%.1f%%\n",sameNum*100.0/totalNum);
    }
    return 0;
}

其他大神的优秀代码:

柳神:https://www.liuchuo.net/archives/2106

解题思路:直接使用set存储每一个集合,然后把set放进一个数组里面存储。当需要计算集合a和集合b的相似度nc和nt的时候,遍历集合a中的每一个元素,寻找集合b中是否有该元素,如果有,说明是两个人公共的集合元素,则nc++,否则nt++(nt的初值为b集合里面本有的元素)

#include <cstdio>
#include <vector>
#include <set>
using namespace std;
int main() {
    int n, m, k, temp, a, b;
    scanf("%d", &n);
    vector<set<int>> v(n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &m);
        set<int> s;
        for(int j = 0; j < m; j++) {
            scanf("%d", &temp);
            s.insert(temp);
        }
        v[i] = s;
    }
    scanf("%d", &k);
    for(int i = 0; i < k; i++) {
        scanf("%d %d", &a, &b);
        int nc = 0, nt = v[b-1].size();
        for(auto it = v[a-1].begin(); it != v[a-1].end(); it++) {
            if(v[b-1].find(*it) == v[b-1].end())
                nt++;
            else
                nc++;
        }
        double ans = (double)nc / nt * 100;
        printf("%.1f%%\n", ans);
    }
    return 0;
}

昵称五个字:https://blog.csdn.net/a617976080/article/details/95910891

解题思路:对每个集合使用一个set来存储它的数组。对于每个查询,遍历一个set的所有元素是否在另一个set中即可得到相同元素个数。即得相似度。(对指针的使用相当纯熟,学习了!!)

#include <cstdio>
#include <set>
using namespace std;
#define MAX_N 50
int main() {
    // 读取数据
    int N;
    scanf("%d", &N);
    set<int> data[MAX_N + 1];
    for(int i = 1, l, t; i <= N; i++){
        scanf("%d", &l);
        while (l-- > 0) {
            scanf("%d", &t);
            data[i].insert(t);
        }
    }
    
    // 输出结果
    int K, p, q, cnt;
    set<int> *pSet, *qSet;
    scanf("%d", &K);
    while (K-- > 0) {
        scanf("%d %d", &p, &q);
        cnt = 0;
        pSet = &data[p];
        qSet = &data[q];
        for(auto lt = qSet->cbegin(); lt != qSet->cend(); lt++){
            if(pSet->count(*lt) == 1) cnt++;
        }
        // Nt为两个set的和减去公共的元素个数
        printf("%.1f%%\n", (float)cnt * 100 / (qSet->size() + pSet->size() - cnt));
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值