思路
- 1.首先要知道
c++
里面有个set
,他可以保存不重复的数字,这样就好办多了,set
数组的用法为:set<int> a[52]
。 - 2.接着要找出里面相同的项,对于在
u
中取出的每个数,然后再v
中去找,看能不能找到,能找到则加1,这里用到了.find()
函数。 - 3.输出就行了。
我出错的一些点
- 1.首先不知道
set
怎么用的吧,他可以看.size()
可以.find()
,可以用迭代器set<int>::iterator it;
。 - 2.输出的时候因为是百分数,居然忘了乘以100了。
代码
#include <iostream>
#include<vector>
#include<set>
#include<iomanip>
using namespace std;
set<int> a[52];
int main()
{
int n;
cin >> n;
int m,value;
for (int i = 1; i <= n; i++)
{
cin >> m;
while (m)
{
cin >> value;
a[i].insert(value);
m--;
}
}
int k,u,v;
cin >> k;
while (k--)
{
cin >> u>>v;
set<int>::iterator it;
int cnn = 0;
for (it = a[u].begin(); it != a[u].end(); it++)
{
if (a[v].find(*it) != a[v].end())
{
cnn++;
}
}
cout << fixed << setprecision(1) << (float)cnn*100 / (a[v].size() + a[u].size() - cnn) << "%" << endl;
}
return 0;
}