【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 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<iostream>
#include<set>
#include<vector>
using namespace std;
int main()
{
int N;
int i,count,j,t;
cin>>N;
vector< set<int> > vec(N);
for(i=0; i<N; i++){
scanf("%d",&count);
for(j=0; j<count; j++){
scanf("%d",&t);
vec[i].insert(t);
}
}
int request,a,b;
scanf("%d",&request);
for(i=0; i<request; i++){
scanf("%d %d",&a,&b);
set<int> temp_set;
set<int>::iterator it;
for(it = vec[a-1].begin(); it != vec[a-1].end(); it++){
temp_set.insert( *it );
}
for(it = vec[b-1].begin(); it != vec[b-1].end(); it++){
temp_set.insert( *it );
} printf( "%.1f",(float)( vec[a-1].size()+vec[b-1].size()-temp_set.size())*100/(float)(temp_set.size()) );
cout<<"%"<<endl;
}
return 0;
}
【PAT】1063. Set Similarity (25) 待改进的更多相关文章
- PAT 1063 Set Similarity (25)
题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...
- 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 (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 (Advanced Level) 1063. Set Similarity (25)
读入之后先排序. 询问的时候可以o(m)效率得到答案. #include<cstdio> #include<cstring> #include<cmath> #in ...
- PAT甲题题解-1063. Set Similarity (25)-set的使用
题意:两个整数集合,它们的相似度定义为:nc/nt*100%nc为两个集合都有的整数nt为两个集合一共有的整数注意这里的整数都是各不相同的,即重复的不考虑在内.给出n个整数集合,和k个询问,让你输出每 ...
- 【PAT甲级】1063 Set Similarity (25 分)
题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...
- 1063 Set Similarity (25分)
Given two sets of integers, the similarity of the sets is defined to be /, where Nc is the number ...
随机推荐
- linux 线程的同步 三 (内存信号量的使用)
信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过是同步的对象不同而已.但是下面介绍的信号量的接口是用于线程的信号量,注意不要跟用于进程间通信的信号量混淆,关于用于进程间通 ...
- 编译sass,遇到报错error style.scss (Line 3: Invalid GBK character "\xE5")
今天学习sass,写了一行中文注释,结果却遇到了报错: cmd.exe /D /C call C:/Ruby23-x64/bin/scss.bat --no-cache --update style. ...
- Java进行spark计算
首先在Linux环境安装spark: 可以从如下地址下载最新版本的spark: https://spark.apache.org/downloads.html 这个下载下来后是个tgz的压缩包,解压后 ...
- HDU 4586 Play the Dice(数学期望)
Play the Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 安装特定版本 sysbench 以及安装时指定特定的mysql客户端
step1: 下载安装源码 https://github.com/akopytov/sysbench (git clone https://github.com/akopytov/sysbench.g ...
- [JS]常用特效js插件
网站一:http://www.superslide2.com/ function set_a_title(n) { var t = $.trim($(n).text()); t && ...
- [转]Acrylic DNS Proxy 使用方法
本文转自:http://www.cnwyw.net/index.php/acrylic-dns-proxy-ping-bi-guang-gao/ 从开始菜单进行“Edit Configuration ...
- 年薪30万的Android程序员必须知道的帖子
https://github.com/Trinea/android-open-project
- MySQL my.cnf参数配置优化详解
[b]PS:本配置文件针对Dell R710,双至强E5620.16G内存的硬件配置.CentOS -100-300w的站点,主要使用InnoDB存储引擎.其他应用环境请根据实际情况来设置优化.[/b ...
- 公司培训lesson 1-代码质量
课堂讲义 代码质量五大原则 编码规范 命名规范.学会合理科学的命名类名.方法名.变量名.命名宗旨:简洁明了,见名只意:了解常用单词缩写 注释.是否写根据所处工作环境的需要而决定.将以源代码写成以“注释 ...