Given two sets of integers, the similarity of the sets is defined to be /, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ 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%
33.3%
题目分析:利用map对应存储需要的键与值 然后从键少的那个遍历 看另一个map中有没有对应的值 注
不能通过直接访问来判断某个键值是否存在(在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);
}
}
												

最新文章

  1. 触屏手机3G网站设计
  2. C语言中内存对齐
  3. 8.31 js基础总结1
  4. 【XLL 框架库函数】 debugPrintf
  5. python中的装饰器
  6. 查看android app 线程信息的命令
  7. Phoenix Framework对于Tree该方法节点设置不同的图标,每个
  8. runtime——消息机制
  9. secureCRT端口转发功能
  10. Disruptor深入解读
  11. C#通过OpenCL调用显卡GPU做高效并行运算
  12. javascript中的Promise使用
  13. bzoj1069 [SCOI2007]最大土地面积 旋转卡壳
  14. SSM-Spring-01:Spring的概念+入门案例
  15. Vue 学习笔记 — 组件初始化
  16. WLST
  17. redis Lua学习与坑
  18. DELPHI中MDI子窗口的关闭 和打开
  19. Python 12 - Mysql &amp; ORM
  20. Hangfire 任务调度

热门文章

  1. optimizing Wi-Fi solution for International School
  2. Spark入门(三)--Spark经典的单词统计
  3. Java 基础(四):数组
  4. javaScript 基础知识汇总 (十一)
  5. 快速排序--15--快排--LeetCode排序数组
  6. java web 中base64传输的坑
  7. BeanShell调用自己写的jar包进行MD5加密
  8. scrapy爬虫提取网页链接的两种方法以及构造HtmlResponse对象的方式
  9. Spring Boot入门系列(九)如何实现异步执行任务
  10. rbac权限(2)