zoukankan      html  css  js  c++  java
  • PAT 1063. Set Similarity

    1063. Set Similarity

    题目大意

    给定 n 个集合, k 个询问, 求任意两个集合的并集和合集.

    思路

    一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 vector勉强过了, 最后才发现原来如此简单.

    代码

    #include <cstdio>
    #include <set>
    #include <vector>
    using namespace std;
    int main(){
        int nSet;
        scanf("%d", &nSet);
        vector<set<int> > sts(nSet + 1);
        for(int i = 1; i <= nSet; i++){
            int nNum;
            scanf("%d", &nNum);
            set<int> temp;
            for(int j = 0; j < nNum; j++){
                int val;
                scanf("%d", &val);
                temp.insert(val);
            }
            sts[i] = temp;
        }
        int nQuery;
        scanf("%d", &nQuery);
        for(int i = 0; i < nQuery; i++){
            int a, b;
            scanf("%d %d", &a, &b);
            int Nc = 0;
            int Nt = sts[a].size() + sts[b].size();
            for(set<int>::iterator ita = sts[a].begin(); ita != sts[a].end(); ++ita){
                if(sts[b].find(*ita) != sts[b].end()){
                    Nc++; Nt--;
                }
            }
            printf("%.1f%%
    ", Nc * 100.0 / Nt);
        }
        return 0;
    }
    
    
  • 相关阅读:
    python 按行读取判断是否为空
    python获取目录下所有文件
    Kolakoski
    最小背包问题
    python 求第k个最大数
    python 求最大子序列
    爬取数据的程序
    文件对比程序
    trsd_extract_EDSD_new
    tred_extract_EDED_new
  • 原文地址:https://www.cnblogs.com/1pha/p/7799756.html
Copyright © 2011-2022 走看看