集合相似度-easy-stl-set

标签:result   函数   知识点   去重复   name   cin   pac   printf   pre   

#include<iostream>
#include<cstdio>
#include<set>
#define MAXSIZE 51
using namespace std;
set<int> s[MAXSIZE];/*建立set数组,自动除去重复的并且升序排好,然后利用count函数即可,知识点:stl-set*/
double Same(int t1, int t2) {
	double result = 0.0;
	int counter = 0;
	for (auto it = s[t1].begin(); it != s[t1].end(); ++it) {
		if (s[t2].count(*it))//count函数
			++counter;
	}
	result = (counter * 1.0) / (s[t1].size() + s[t2].size() - counter);
	return result;
}
int main() {
	int N, K;
	cin >> N;
	for (int i = 1, num; i <= N; ++i) {
		cin >> num;
		for (int j = 0, temp; j < num; ++j) {
			cin >> temp;
			s[i].insert(temp);
		}
	}
	cin >> K;
	for (int i = 0, t1, t2; i < K; ++i) {
		cin >> t1 >> t2;
		printf("%.2f%%\n", 100 * Same(t1, t2));
	}
	return 0;
}

集合相似度-easy-stl-set

标签:result   函数   知识点   去重复   name   cin   pac   printf   pre   

原文地址:https://www.cnblogs.com/am6a6/p/13938597.html

版权声明:完美者 发表于 2020-11-07 16:42:41。
转载请注明:集合相似度-easy-stl-set | 完美导航

暂无评论

暂无评论...