【PAT】A1063 Set Similarity
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 Nlines 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:
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个序列和其中的数,要求给出指定两个集合的(相同个数)/(不同个数)的比率
- 使用STL中的set容器,因为set容器自动去掉重复的数并且按升序排序集合里的数。代码分为2段:首先按照要求输入数据,即输入“要输入的集合数n”和“n个集合,每个集合要输入的数m,以及输入m个数”,用set的insert()函数可以过滤掉重复的数据;下一步是整理数据,例如统一确定st[set2]是被查找的集合,循环st[set1],如果找到st[set2].find(*it)!=st[set2].end(),意思是非空,也就是找到了,就相同数加一,反之就是st[set1]中有的数在st[set2]中是没有的,那么把st[set2].size()++,每一次输出即可。
- 总结:在st[set2].find(*it)!=st[set2].end(),这里需要思路清晰,大脑不能犯糊涂,对照1查2,2中查不到就++,不是对照1查1
代码:
#include <stdio.h>
#include <set>
using namespace std;
set<int> st[55];int main(){int n,m,x,set1,set2,inquiry,same,count=0;scanf("%d",&n); //集合的数量n//输入数据 for(int i=1;i<=n;i++){scanf("%d",&m); //集合中的数量mfor(int j=0;j<m;j++){scanf("%d",&x); //集合中的每一个数,赋值为xst[i].insert(x); //将数插入到第i个集合中 } }//整理数据scanf("%d",&inquiry); //需要查询的次数for(int i=0;i<inquiry;i++){same=0;scanf("%d%d",&set1,&set2); //比较的集合编号int count=st[set2].size();for(set<int>::iterator it=st[set1].begin();it!=st[set1].end();it++){//从第一个集合中的每一个数,看是否有数和st[set2]中的数一致,一致就same++,不一致则count++if(st[set2].find(*it)!=st[set2].end()){//一开始弄错的地方same++;//printf("%d\n",same);}else{count++;//printf("c%d\n",count);}}printf("%.1lf%%\n",(double)(same*100.0/count));}return 0;
}
【PAT】A1063 Set Similarity相关推荐
- 【PAT】乙级题目解答合集(c++)
[PAT]乙级题目解答合集(c++) 本篇文章为对PAT乙级1001-1095的题目解答的汇总 1001 害死人不偿命的(3n+1)猜想 (15 分) 1002 写出这个数 (20 分) 1003 我 ...
- 【PAT】第四章 算法初步
第四章 算法初步 目录 第四章 算法初步 4.1 排序 4.1.1 选择排序 4.1.2 插入排序 4.1.3 排序题与sort函数的应用 strcmp 计算排名 4.2 散列 4.2.1 散列 1. ...
- 【PAT】PAT总结《搜索、打表、分治、排序算法、队列、栈、堆、Hash》
打表 打表这个技巧其实我们在素数部分的题已经涉及到了.还是要仔细思考,什么时候应该打表,打表有助于减少后续的计算的时候,我们应该打表, 特别是查询量特别大,即时计算无论多么快都会超时的情况.或者逆向运 ...
- 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)
在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...
- 【PAT】(B)1094 谷歌的招聘 (20 分)
『题意描述』 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com 结尾的网址,而前面的网址是一个 10 位素数,这个素数是 ...
- 【PAT】A1090 Highest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 【PAT】A1079 Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- 【PAT】1091 Acute Stroke (30 分)
三维搜索,按照6个邻接理论,总会有重合的部分区域, 因此根据搜索顺序,先访问到的就是一个区域的[不能较真] #include <bits/stdc++.h> using namespace ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
最新文章
- python中对象及对象引用--傻傻分不清
- 论坛报名 | AI赋能未来交通
- java8 lambda 排序算法,Java8中排序算法比较器的三种写法(使用lambda表达式实现Comparator比较器)...
- 解题:USACO15JAN Grass Cownoisseur
- c#类属性和实例属性_Visual C#类和对象的创建方式,定义类,实例化对象,实例讲解...
- pgadmin连接服务器失败_增值税发票税控软件:连接服务器失败是否使用离线文件进行更新?...
- freemark循环map_java与freemarker遍历map
- VMware下CentOS6.8配置GFS文件系统
- 转载:PLSQL中显示Cursor、隐示Cursor、动态Ref Cursor区别
- 2021华为悦盒EC6110-T-M-拆机-强刷固件及教程
- java 素数 五行_(1)转载:八卦数论(二)
- 快捷连接 残差_残差网络解决了什么问题
- 魔兽世界服务器维护有哪些内容,魔兽世界wow服务器实装维护公告内容详情介绍_魔兽世界维护公告_快吧游戏...
- 在div中加本地html,div加载另一个HTML页面
- ubuntu 自动切换壁纸
- 企业 SDLC 安全生命周期管理
- Multimodal Sentiment Analysis论文汇总
- 听书笔记:《勇敢去敲老板的门》
- xcode9 symbolicatecrash文件位置
- k3s 搭建高可用rancher