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 ...
 
随机推荐
- Mysql DELETE 不能使用别名? 是我不会用!
			
今天碰到一个sql问题,就是在delete中加了别名,导致报错了:"[Err] 1064 - You have an error in your SQL syntax; ..." ...
 - 使用宏定义来判断是a和b 的大小
			
#include <stdio.h> #include <math.h> #define MAX(a, b) (a) > (b) ? printf("a > ...
 - 一次vaccum导致的事故
			
1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...
 - Visual Studio Code IDE开发插件配置
			
[PHP通用集成环境] PHP Extension Pack #PHP拓展包,PHP开发最重要的拓展 PHP Intelephense #PHP自动补全工具 PHP IntelliSense #PHP ...
 - Nutch2.1+mysql+solr3.6.1+中文网站抓取
			
1.mysql 数据库配置 linux mysql安装步骤省略. 在首先进入/etc/my.cnf (mysql为5.1的话就不用修改my.cnf,会导致mysql不能启动)在[mysqld] 下添加 ...
 - 关于windows中 redis 闪退问题
			
就在刚刚,因为 Redis 闪退原因,搞了快半小时,电脑关机前还能用,关机后一打开就秒退,所以我先发个解决方案再继续码.. 按照步骤一步一步来 ( 设置 redis 密码看文章最后 ) 给你省个事,我 ...
 - 12-Vue的使用-安装 - 条件渲染
			
一.安装 1. 去vue官网: https://cn.vuejs.org/ 2. 引入vue.js文件 <body> <script src="vue.js"& ...
 - php  MySQL 选择数据库
			
在你连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简单的选择特定的数据库.你 ...
 - [转]C++ 模板 静态成员 定义(实例化)
			
提出问题: 如果有这样一个模板: template <typename T> class Test{ public: static std::string info; }; 对于以下若干种 ...
 - Centos7的rabbitmq镜像集群
			
1.下载RabbitMQ vim /etc/hosts10.10.21.197 rabbit110.10.21.198 rabbit2 #分别命名hostname rabbit1hostname ra ...