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 3
Sample Output:
50.0%
33.3%
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
set<int> num[];
int main(){
int N, M, temp, K;
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &M);
for(int j = ; j < M; j++){
scanf("%d", &temp);
num[i].insert(temp);
}
}
scanf("%d", &K);
int s1, s2;
for(int i = ; i < K; i++){
scanf("%d %d", &s1, &s2);
int same = , diff = num[s2].size();
for(set<int> ::iterator it = num[s1].begin(); it != num[s1].end(); it++){
if(num[s2].find(*it) != num[s2].end())
same++;
else diff++;
}
double rate = 1.0 * same / diff * 100.0;
printf("%.1lf%%\n", rate);
}
cin >> N;
return ;
}
总结:
1、set可以用于hash表解决不了的情况,比如键值不是int的情况。
2、printf输出%时,需要输出%%。
1063. Set Similarity的更多相关文章
- 1063. Set Similarity (25)
1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 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 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- 1063 Set Similarity——PAT甲级
1063 Set Similarity Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*10 ...
- 【PAT】1063. Set Similarity (25) 待改进
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...
- PAT 甲级 1063 Set Similarity
https://pintia.cn/problem-sets/994805342720868352/problems/994805409175420928 Given two sets of inte ...
- 1063 Set Similarity (25分)
Given two sets of integers, the similarity of the sets is defined to be /, where Nc is the number ...
- PAT 1063 Set Similarity (25)
题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...
随机推荐
- Mvc_前后端绑定数据json集合
ViewBag.SysModuleList =new List<SysModule>(){.....}; var data = @Html.Raw(Json.Encode(ViewBag ...
- netfilter/iptables 简介
netfilter 是 Linux 内置的一种防火墙机制,我们一般也称之为数据包过滤机制.iptables 则是一个命令行工具,用来配置 netfilter 防火墙.下图展示了一个带有防火墙的简单网络 ...
- MongoDB集群运维笔记
前面的文章介绍了MongoDB副本集和分片集群的做法,下面对MongoDB集群的日常维护操作进行小总结: MongDB副本集故障转移功能得益于它的选举机制.选举机制采用了Bully算法,可以很方便从分 ...
- spring中通过JNDI、DBCP、C3P0配置数据源
JNDI配置数据源 1.首先在tomcat的server.xml中配置数据源信息,找到Context,然后在里边加入如下代码 <Context docBase="SpringDemo& ...
- windows下net命令失败
D:\apache-tomcat-7.0.57\bin>net start mysql57发生系统错误 5. 拒绝访问. 以管理员身份运行 run as administrator 打开cmd. ...
- Java关于struts2框架
今天研究了一下struts2框架,我不太喜欢理论的东西,我研究框架更喜欢打断点一步步跟着去看实现的过程.
- C#.字符串转数组,数组转字符串
string str = "1,2,3,4,5,6,7"; string[] strArray = str.Split(','); //字符串转数组 str = string.Em ...
- Bootstrap媒体对象
前面的话 在Web页面或者说移动页面制作中,常常看到图文混排效果,图片居左(或居右),内容居右(或居左)排列.常常把这样的效果称为媒体对象.可以说它是一种抽象的样式,可以用来构建不同类型的组件.本文将 ...
- jenkins--svn+Email自动触发3(jenkins全局设置)
全局java配置: 全局sonar-scanner插件配置:
- HDU4287-STL模拟水题
一场2012天津网络预选赛的题,签到题. 但是还是写了三四十分钟,C++和STL太不熟悉了,总是编译错误不知道怎么解决. 一开始用的Char [] 后来改成了string,STL和string搭配起来 ...