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的性质 ...
随机推荐
- CVE-2018-4407(IOS缓冲区溢出漏洞)exp
CVE-2018-4407为ios缓冲区溢出漏洞 exp: import scapyfrom scapy.all import * send(IP(dst="同一局域网内目标Ip" ...
- chm打开看不到内容时好时坏
右击chm文件,属性====>解除锁定 再打开 ok
- io流读写操作
/** * * DOC 将F盘下的test.jpg文件,读取后,再存到E盘下面. * * @param args * @throws Exception */ public static void m ...
- Pytest(4)失败重跑插件pytest-rerunfailures
安装: pip3 install pytest-rerunfailures 重新运行所有失败用例 要重新运行所有测试失败的用例,请使用--reruns命令行选项,并指定要运行测试的最大次数: $ py ...
- mitmproxy使用详解
mitmproxy 相比Charles.fiddler的优点在于,它可以命令行方式或脚本的方式进行mock mitmproxy不仅可以像Charles那样抓包,还可以对请求数据进行二次开发,进入高度二 ...
- Codeforces 1355 E. Restorer Distance(三分)
传送门:E - Restorer Distance 题意:给出四个数 N, A, R, M ,然后给出一个长度为N的序列.让一个数+1花费A,-1花费R,从一个大的数向一个小的数移动1花费M.问让所 ...
- P1280 尼克的任务(DP)
题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为N分钟,从第一分钟开始 ...
- poj2778 DNA Sequence(AC自动机+矩阵快速幂)
Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...
- VJ train1 I-彼岸
一道递推题(我这个菜鸡刚开始以为是排列组合) 题目: 突破蝙蝠的包围,yifenfei来到一处悬崖面前,悬崖彼岸就是前进的方向,好在现在的yifenfei已经学过御剑术,可御剑轻松飞过悬崖.现在的问题 ...
- Codeforces Round #171 (Div. 2) B. Books (模拟队列)
题意:有一组数,问子数组和最大不超过\(t\)的最多元素个数. 题解:用数组模拟队列,不断的往里面放,队列中的元素之和大于\(t\),就不断地从队头弹出直到满足条件,维护一个最大值即可. 代码: in ...