编写一个C++代码给定两个整数集合它们的相似度定义为:N c N t ×100。其中N c 是两个集合都有的不相等整数的个数N t 是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。输入格式:输入第一行给出一个正整数N≤50是集合的个数。随后N行每行对应一个集合。每个集合首先给出一个正整数M≤10 4 是集合中元素的个数;然后跟M个010 9 区间内的整数。之后
#include
int main() {
int N;
cin >> N;
vector<set
int K;
cin >> K;
for (int i = 0; i < K; i++) {
int a, b;
cin >> a >> b;
int Nc = 0;
for (int num : sets[a-1]) {
if (sets[b-1].count(num) > 0) {
Nc++;
}
}
int Nt = sets[a-1].size() + sets[b-1].size() - Nc;
double similarity = (double)Nc / Nt * 100;
cout << fixed << setprecision(2) << similarity << "%" << endl;
}
return 0;
原文地址: https://www.cveoy.top/t/topic/hylD 著作权归作者所有。请勿转载和采集!