PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
Given two sets of integers, the similarity of the sets is defined to be /, 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 (≤) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤) and followed by M integers in the range [0]. After the input of sets, a positive integer K (≤) is given, followed by Klines 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%
题意:
本题定义集合相似度Nc/Nt*100%,其中Nc是两个集合中共有的不相等元素个数,Nt是两个集合中不相等元素的个数。你的任务是计算给定的集合的相似度。
Nc是两个集合的交集元素数量。
Nt是两个集合的并集元素数量。
题解:
本来用数组,明显内存超限,速学了set,很方便啊!!!一下子过了。
printf 输出%,要%%
AC代码:
#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
set<int>s[];
int n,m,k;
int main(){
cin>>n;
for(int i=;i<=n;i++){
cin>>m;
for(int j=;j<=m;j++){
int x;
cin>>x;
s[i].insert(x);
}
}
cin>>k;
for(int i=;i<=k;i++){
int a,b;
cin>>a>>b;
set<int>::iterator it;
int count=;
for(it=s[a].begin();it!=s[a].end();it++){
if(s[b].find(*it)!=s[b].end()) count++;
}
printf("%.1f%%\n",count*1.0/(s[a].size()+s[b].size()-count)*);
}
return ;
}
PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)的更多相关文章
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
随机推荐
- Robot Framework--Scalar变量
一.变量赋值 1)Set赋值 通常使用Set Variable关键字对变量进行赋值,其他Set相关的带Variable的关键字也可以进行赋值 赋值的时候,变量后面写不写『=』都可以,如下: 如果${v ...
- 编写一个c程序来计算整数中的设置位数?
回答: unsigned int NumberSetBits(unsigned int n) { ; while (n) { ; ; } return CountSetBits; } 本质上就是计算n ...
- Mysql开启慢查询命令
; 查询: SHOW VARIABLES LIKE '%slow_query_log%'; 结果:
- mongodb 4.0.5 集群搭建五台集群
配置文件采用yaml方式来配置 生产中取消了仲裁者的角色,因为仲裁者也不会存储数据,只是起到选举的作用,线上为了保证数据安全,每份数据都会配置两个副本集,也就是每份数据存储了三份. 优化配置,采用五台 ...
- 062_判断用户输入的是 Yes 或 NO
#!/bin/bashread -p "Are you sure?[y/n]:" surecase $sure iny|Y|Yes|YES) echo "you ...
- Python基础之定义有默认参数的函数
1. 构建有默认参数的函数 当我们在构建一个函数或者方法时,如果想使函数中的一个或者多个参数使可选的,并且有一个默认值,那么可以在函数定义中给参数指定一个默认值,并且放到参数列表的最后就行了.比如: ...
- LG5492 [PKUWC2018]随机算法
题意 有一种贪心求最大独立集的算法: 随机一个排列 按顺序加入独立集,如果一个点能加入,就加入\({S}\) 给出一张图,问得出正确答案的概率. \(n \leq 20\) 传送门 思路 用 \(dp ...
- [信息收集]Nmap命令详解
0x00[介绍] Nmap,也就是Network Mapper,中文为"网络映射器". Nmap是一款开源的网络探测和安全审核的工具,它的设计目标是快速地扫描大型网络. 它是网络管 ...
- iOS UILabel镂空特效
http://stackoverflow.com/questions/18716751/drawing-a-path-with-subtracted-text-using-core-graphics ...
- mac下生成keystore
keytool -genkey -v -keystore sign.keystore -alias cxa -keyalg RSA -validity 20000 -keystore /Users/c ...