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的性质 ...
随机推荐
- RESTFul应用分析
Restful API 近年来应用越来越广泛,各大互联网公司纷纷推出了自己的 Restful API 服务. 本文将从实际应用出发,从 REST 到 Restful 再到 Restful API ,逐 ...
- ASP.NET Core默认容器实现Controller的属性注入
仅针对Controller的属性注入: 使用默认容器,不依赖第三方库: 故事背景 闲来无事给项目做优化,发现大多数Controller里面都会用到Logger和AutoMapper,每个Contr ...
- linux(9)find命令详解
find命令格式: find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数: path:要查找的目录路径. ~ 表示$HO ...
- 2019牛客暑期多校训练营(第五场)H-subsequence 2 (拓扑排序+思维)
>传送门< 题意: 给你几组样例,给你两个字符a,b,一个长度len,一个长度为len的字符串str,str是字符串s的子串 str是s删掉除过a,b两字符剩下的子串,现在求s,多种情况输 ...
- Codeforces Round #652 (Div. 2) B. AccurateLee(字符串)
题目链接:https://codeforces.com/contest/1369/problem/B 题意 给出一个长 $n$ 的 二进制串,每次可以选择字符串中的一个 $10$,然后删除其中的一个字 ...
- Buy the Ticket HDU - 1133 大数dp
题意: 演唱会门票售票处,那里最开始没有零钱.每一张门票是50元,人们只会拿着100元和50元去买票,有n个人是拿着50元买票,m个人拿着100元去买票. n+m个人按照某个顺序按序买票,如果一个人拿 ...
- P1387 最大正方形 && P1736 创意吃鱼法(DP)
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...
- CodeForces - 1100E 二分+拓扑排序
题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...
- hdu5360 Hiking
Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, want ...
- [Python] Pandas的delete、drop函数的用法
目录 drop函数 Axis(轴)含义 drop用法实验 delete函数 drop函数 DataFrame.drop(labels=None, axis=0, index=None, columns ...