【PTA】【L2-005】集合相似度(25分)

【PTA】【L2-005】集合相似度(25分)

 题意:求给定两个集合的 差集大小 并集大小。

思路:使用set内置函数。

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<set>
#include<iomanip>
using namespace std;


int main()
{
	ios::sync_with_stdio(false);
	vector<set<int> >s;
	int n, k;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int m; cin >> m;
		set<int>t;
		for (int j = 0; j < m; j++)
		{
			int x; cin >> x;
			t.insert(x);
		}
		s.push_back(t);
	}

	cin >> k;
	while (k--)
	{
		int x, y;
		cin >> x >> y;
		x--, y--;  // 下标对应
		set<int>cnt1(s[x]), cnt2(s[y]);
		vector<int>A, B;

		// 差集
		set_intersection(cnt1.begin(), cnt1.end(), cnt2.begin(), cnt2.end(), insert_iterator<vector<int> >(A, A.begin()));

		//并集
		set_union(cnt1.begin(), cnt1.end(), cnt2.begin(), cnt2.end(), insert_iterator<vector<int> >(B, B.begin()));

		cout << fixed << setprecision(2) << double(A.size()) / B.size() * 100 << "%\n";
	}
	return 0;
}

上一篇:哈理工新生赛补题


下一篇:【洛谷P5047】Yuno loves sqrt technology II