1063 Set Similarity——PAT甲级
1063 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 N lines 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 3Sample Output:
50.0%
33.3%
题目大意:定义集合的相似度为Nc / Nt * 100%,其中Nc为两个集合相同数字的个数,Nt为两个集合不同数字的总数。给出N个集合,然后有K次查询,让你求出所查两个集合的相似度。
大致思路:先利用set存储每个集合,然后执行K次查询是利用find函数来查找集合相同元素。
代码:
#include <bits/stdc++.h>
using namespace std;
int n,k;
int main() {
scanf("%d", &n);
set<int> se[n + 1];
for (int i = 0; i < n; i++) {
int m; scanf("%d", &m);
for (int j = 0; j < m; j++) {
int x; scanf("%d", &x);
se[i + 1].insert(x);
}
}
scanf("%d",&k);
for (int i = 0; i < k; i++) {
int q1, q2; scanf("%d%d",&q1, &q2);
double Nc = 0.0, Nt = 0.0;
for (set<int> :: iterator ite = se[q1].begin() ; ite != se[q1].end(); ite++) {
if (se[q2].find(*ite) != se[q2].end()) Nc++;
}
Nt = (se[q1].size() + se[q2].size()) * 1.0 - Nc;
double ans = Nc / Nt * 100.0;
printf("%.1lf",ans);puts("%");
}
return 0;
}
1063 Set Similarity——PAT甲级的更多相关文章
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 1063 Set Similarity[比较]
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...
- PAT 1063. Set Similarity
1063. Set Similarity 题目大意 给定 n 个集合, k 个询问, 求任意两个集合的并集和合集. 思路 一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
随机推荐
- 在线安装mysql
http://www.cnblogs.com/wishwzp/p/7113403.html
- dedecms后台更新网站栏目无反应的解决方法
dedecms进行第二次模板开发后,遇到在栏目更新的时候没有反应,但是用回原来的初始模板就可以,百度查找了很多的教程也无法进行解决,就这样慢慢的摸索.终于找到了问题的所在,原因可能是该更新的时候无法获 ...
- 小白搭建WAMP详细教程---php安装与设置
一.php官网下载php压缩包 到php官网http://www.php.net 下载,有很多版本,我们这里选择7.2.25,具体步骤如下: 二.php的安装 下载后得到如下的压缩包,将压缩包解压到您 ...
- Flink 在又拍云日志批处理中的实践
日前,由又拍云举办的大数据与 AI 技术实践|Open Talk 杭州站沙龙在杭州西溪科创园顺利举办.本次活动邀请了有赞.个推.方得智能.又拍云等公司核心技术开发者,现场分享各自领域的大数据技术经验和 ...
- CyclicBarrier---可重用的循环栅栏
很多文章只是提了下可重用,具体这个栅栏怎么可重用的,很多没有说明,这里会解开你的疑惑. CyclicBarrier是一个同步工具类,它允许一组线程互相等待,直到达到某个公共屏障点.与CountDown ...
- Flink-v1.12官方网站翻译-P013-Timely Stream Processing
及时的流处理 介绍 及时流处理是有状态流处理的一种扩展,其中时间在计算中起着一定的作用.其中,当你做时间序列分析时,当做基于某些时间段(通常称为窗口)的聚合时,或者当你做事件处理时,事件发生的时间很重 ...
- 【noi 2.6_9284】盒子与小球之二(DP)
题意:有N个有差别的盒子和分别为A个和B个的红球和蓝球,盒子内可空,问方案数. 解法:我自己打的直接用了求组合C的公式,把红球和蓝球分开看.对于红球,在N个盒子可放任意个数,便相当于除了A个红球还有N ...
- Relatives POJ - 2407 欧拉函数
题意: 给你一个正整数n,问你在区间[1,n)中有多少数与n互质 题解: 1既不是合数也不是质数(1不是素数) 互质是公约数只有1的两个整数,叫做互质整数.公约数只有1的两个自然数,叫做互质自然数 所 ...
- 避坑!js正确地使用fill()初始化二维数组
先介绍一下坑 fill()方法都知道,填充数组 比如: let a = new Array(5).fill(0); console.log(a); // 输出结果为[0, 0, 0, 0, 0] 当我 ...
- redis如何实现高可用【主从复制、哨兵机制】
实现redis高可用机制的一些方法: 保证redis高可用机制需要redis主从复制.redis持久化机制.哨兵机制.keepalived等的支持. 主从复制的作用:数据备份.读写分离.分布式集群.实 ...