当前位置:网站首页>L2-005 集合相似度(vector、set求并交集)
L2-005 集合相似度(vector、set求并交集)
2022-07-26 10:26:00 【真题OK撒】
给定两个整数集合,它们的相似度定义为:Nc/Nt×100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。
输入格式:
输入第一行给出一个正整数N(≤50),是集合的个数。随后N行,每行对应一个集合。每个集合首先给出一个正整数M(≤104),是集合中元素的个数;然后跟M个[0,109]区间内的整数。
之后一行给出一个正整数K(≤2000),随后K行,每行对应一对需要计算相似度的集合的编号(集合从1到N编号)。数字间以空格分隔。
输出格式:
对每一对需要计算的集合,在一行中输出它们的相似度,为保留小数点后2位的百分比数字。
输入样例:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
输出样例:
50.00%
33.33%#include <iostream>
#include <algorithm>
#include <vector>
// #include <iterator>
using namespace std;
const int N = 60;
vector<int>v[N];
int main()
{
int n; scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int k; scanf("%d", &k);
while (k--)
{
int x; scanf("%d", &x);
v[i].push_back(x);
}
sort(v[i].begin(), v[i].end());
v[i].erase(unique(v[i].begin(), v[i].end()), v[i].end());
}
scanf("%d", &n);
while (n--)
{
int a, b, cnt = 0; scanf("%d%d", &a, &b);
vector<int> res;
set_intersection(v[a].begin(),v[a].end(),v[b].begin(),v[b].end(),back_inserter(res));
printf("%.2lf%%\n", res.size() * 100.0 /(v[a].size() + v[b].size() - res.size()));
}
return 0;
}
#include <iostream>
#include <set>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
const int N = 60;
set<int>s[N];
int n;
int k, x, a ,b;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> k;
while (k--) cin >> x,s[i].insert(x);
}
cin >> n;
while (n-- )
{
cin >> a >> b;
int cnt = 0;
set<int>:: iterator it;
for (it = s[a].begin(); it != s[a].end(); it++)
if (s[b].find(*it) != s[b].end())
cnt++;
printf("%.2lf%%\n", cnt * 100.0 / (s[a].size() + s[b].size() - cnt));
}
return 0;
}边栏推荐
- 原生JS-获取transform值 x y z及rotate旋转角度
- equals与==的区别
- Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
- 数通基础-STP原理
- [Qualcomm][Network] qti服务分析
- 移动端双指缩放事件(原生),e.originalEvent.touches
- canvas上传图片base64-有裁剪功能-Jcrop.js
- AirTest
- Employee information management system based on Web
- SQL Server 2008 server engine failed to start?
猜你喜欢
随机推荐
Beginner of flask framework-04-flask blueprint and code separation
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
Data communication foundation STP principle
我们的Web3创业项目,黄了
404页面和路由钩子
原生JS-获取transform值 x y z及rotate旋转角度
The problem of four columns of Hanoi Tower
Learning about opencv (3)
Closure of go (cumulative sum)
[C language] LINQ overview
【Halcon视觉】图像滤波
Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
Learning about tensorflow (II)
Introduction to latex, EPS picture bounding box
议程速递 | 7月27日分论坛议程一览
How to write a million reading article
【杂谈】Error loading psycopg2 module :No module named psycopg2
如何写一篇百万阅读量的文章
Function template parameters (where are the function parameters)

![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)


![[qualcomm][network] QTI service analysis](/img/76/49054ff8c7215eca98cc479ab1d986.png)


