1063 Set Similarity (25分)
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 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%题目分析:利用map对应存储需要的键与值 然后从键少的那个遍历 看另一个map中有没有对应的值 注
33.3%
不能通过直接访问来判断某个键值是否存在(在map<int int>的情况下 访问不存在的键 会生成相应的键值对 其值默认为0)要通过map中find函数来判断键值是否存在
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
map<int, int> M[];
int Size[];
int main()
{
int N;
cin >> N;
for (int i = ; i < N; i++)
{
int K;
cin >> K;
for (int j = ; j <K; j++)
{
int num;
cin >> num;
M[i][num]++;
Size[i]++;
}
}
int K;
cin >> K;
for (int i = ; i < K; i++)
{
int v1, v2;
int trueHave = ; //相等元素的个数
cin >> v1 >> v2;
v1--;
v2--;
int size = M[v1].size() + M[v2].size();
int v = (Size[v1] < Size[v2]) ? v1 : v2;
int d = (Size[v1] < Size[v2]) ? v2 : v1;
for (auto it : M[v])
if (M[d].find(it.first)!=M[d].end())
trueHave++;
printf("%.1f%%\n", (1.0 * trueHave) / (1.0 * (size-trueHave))*100.0);
}
}
1063 Set Similarity (25分)的更多相关文章
- 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 (25 分)
题意: 输入一个正整数N表示集合的个数(<=50),接着输入N行,每行包括一个数字x代表集合的容量(<=10000),接着输入x个非负整数.输入一个正整数Q(<=2000),接着输入 ...
- 1063. Set Similarity (25)
1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 【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 (25)
题意:给你n个集合,k次询问,每次询问求两个集合的(交集)/(并集). 思路:k有2000,集合大小有10000.先将每个集合排序,对每个询问分别设两个指针指向两个集合的头.设a[i]为指针1的值,b ...
- 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个询问,让你输出每 ...
- A1063 Set Similarity (25 分)
一.技术总结 这个题目是属于set容器的内容,使用可以减少很多代码量 开始试过在同一个for循环中定义两个auto,结果编译通不过,有时候构思很重要,就比如这一题,开始我是一个一个去加,而代码中是,先 ...
- PAT 1063 Set Similarity[比较]
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be N ...
随机推荐
- RTP SIP win服务端软件 VOIP
RTP Real-time Transport Protocol 实时传输入协议,使用 udp 做为载体. SIP Session Initiation Protocol 会话初始化协议,加入,查询, ...
- php 防注入
a. 打开magic_quotes_gpc或使用addslashes()函数 当php.ini里的 magic_quotes_gpc 为On 时. 提交的变量中所有的 ' (单引号), " ...
- JavaScript 预编译与作用域
JavaScript 预编译与作用域 JavaScript 预编译的过程和作用域的分析步骤是 JS 学习中重要的一环,能够帮助我们知道代码的执行顺序,更好理解闭包的概念 预编译 JavaScript ...
- AVR单片机教程——走向高层
本文隶属于AVR单片机教程系列. 在系列教程的最后一篇中,我将向你推荐3个可以深造的方向:RTOS.C++.事件驱动.掌握这些技术可以帮助你更快.更好地开发更大的项目. 本文涉及到许多概念性的内容 ...
- 如何使用域名访问自己的Windows服务器(Java web 项目)
如何使用域名访问自己的Windows服务器(Java web 项目) 写在前面 前段时间在阿里云弄了个学生服务器,就想着自己搭建一个网站试一试,在网上查阅相关资料时发现大部分都是基于服务器是Linux ...
- 【Weiss】【第03章】练习3.9:大整数运算包
[练习3.9] 编写任意精度的整数运算包,要求使用类似多项式运算的方法.计算24000内数字0到9的分布.
- Js中的For循环详解
大家好,我是逆战班的一员,今天给大家讲解一下Js循环中的For循环. For循环是JS循环中一个非常重要的部分. 我们先讲一下for循环的作用: For循环用在需要重复执行的某些代码,比如从1打印到1 ...
- 2. Plugin execution not covered by lifecycle configuration
问题: 找到当前项目的工作空间下的.metadata\.plugins\org.eclipse.m2e.core路径, 然后添加lifecycle-mapping-metadata.xml文件,内容如 ...
- [并查集+逆向思维]Codeforces Round 722C Destroying Array
Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- [ex-kmp] HDU 2019 Multi-University Training Contest 5-string matching
string matching Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...