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