博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1063 Set Similarity
阅读量:4540 次
发布时间:2019-06-08

本文共 1844 字,大约阅读时间需要 6 分钟。

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 Mintegers 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:

33 99 87 1014 87 101 5 877 99 101 18 5 135 18 9921 21 3

Sample Output:

50.0%33.3%

Nc为两组数组中的公共数字的个数

Nt为去重后,两组数据的总个数

1 #include 
2 #include
//用set来去重 3 using namespace std; 4 int N, M, K, d; 5 unordered_set
nums[55]; 6 int main() 7 { 8 cin >> N; 9 for (int i = 1; i <= N; ++i)10 {11 cin >> M;12 for (int j = 0; j < M; ++j)13 {14 cin >> d;15 nums[i].insert(d);16 }17 }18 cin >> K;19 for (int i = 0; i < K; ++i)20 {21 int a, b;22 double res, Nc = 0, Nt = 0;23 cin >> a >> b;24 for (auto v : nums[a])25 if (nums[b].find(v) != nums[b].end())26 Nc++;27 Nt = nums[a].size() + nums[b].size() - Nc;28 res = (Nc / Nt)*100.0;29 printf("%.1f%%\n", res);30 }31 return 0;32 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11294909.html

你可能感兴趣的文章
Asp.Net Mvc项目添加WebApi
查看>>
三级联动
查看>>
(Delphi)第一个Windows 32 API的窗口程序
查看>>
在Ubuntu16.04里面安装Gogland!
查看>>
ClamAV学习【7】——病毒库文件格式学习
查看>>
[LeetCode] Reverse Nodes in k-Group
查看>>
[LeetCode] Search in Rotated Sorted Array II
查看>>
[STL] lower_bound和upper_bound
查看>>
表单属性enctype="multipart/form-data"
查看>>
数值分析清考给弄纠结了
查看>>
转:GestureDetector: GestureDetector 基本使用
查看>>
小顶堆第二弹-----堆降序排序(C语言非递归)
查看>>
我的一年学习之路
查看>>
mysql优化问题汇总
查看>>
ajax asud模板
查看>>
初识java
查看>>
敏捷、瀑布开发模式
查看>>
类的初始化顺序
查看>>
HDU 2040 亲和数 [补] 分类: ACM 2...
查看>>
实习日记)select option 选择不同的option时, 页面发生不同的变化
查看>>