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的性质 ...
随机推荐
- Spark高级数据分析——纽约出租车轨迹的空间和时间数据分析
Spark高级数据分析--纽约出租车轨迹的空间和时间数据分析 一.地理空间分析: 二.pom.xml 原文地址:https://www.jianshu.com/p/eb6f3e0c09b5 作者:II ...
- MariaDB数据库 ----数据库简介,用户管理,数据库创建,数据类型、数据增删改(实例演示)
数据库简介 数据库--即电子文件柜,用户可以对文件中的数据进行增,删,改,查等操作. 数据库分类 关系型数据库 关系型数据库管理系统(Relational Database Management Sy ...
- 菜鸟初学Linux——Ubuntu系统中,用root权限进行复制粘贴操作
long long ago,积累了一些Linux的小知识,拿出来分享一下,希望以后能够在工作上带来一些帮助. 方法一 第一步:打开终端,在命令行里输入sudo nautilus 第二步:输入你的用 ...
- 学生信息管理系统总结——student数据库中表关系分析
说到关系,那就不得不提两个东西: 1.E-R图,也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法,用来描述现实世界的概念模型 2.关系模 ...
- Linux和Xshell安装
Linux安装 开启虚拟机 选择语言 软件选择 点击软件→软件选择→基础设施服务器 安装位置 网络连接 开始安装 设置root密码 建议设置成root,因为密码忘了改起来很麻烦 登录 查看IP Xsh ...
- 织梦dedecms自由列表的"不使用目录默认主页"错误修正
站长用织梦做站时常常发现织梦自由列表有个致命的问题: 即修改"不使用目录默认主页"就永远不会自己勾选啦 打开这个文件 makehtml_freelist_action.php 搜索 ...
- 一个简单且易上手的 Spring boot 后台管理框架-->EL-ADMIN
一个简单且易上手的 Spring boot 后台管理框架 后台源码 前台源码
- Java基本类型的内存分配在栈还是堆
我们都知道在Java里面new出来的对象都是在堆上分配空间存储的,但是针对基本类型却有所区别,基本类型可以分配在栈上,也可以分配在堆上,这是为什么? 在这之前,我们先看下Java的基本类型8种分别是: ...
- Pytest(12)pytest缓存
前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...
- Kwp2000协议的应用(程序后续篇)
作者:良知犹存 转载授权以及围观:欢迎添加微信:becom_me 总述 接上篇文章,本篇继续对基于PID解析数据,如何依据J1979的标准进行解析数据 先给昨天的文章补上一张故障码对照表,昨天分析了如 ...