https://pintia.cn/problem-sets/994805046380707840/exam/problems/994805070149828608?type=7&page=1
题意:
给定两个整数集合,它们的相似度定义为:Nc/Nt×100%。其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数。你的任务就是计算任意一对给定集合的相似度。
set用法
int n;
cin >> n;
vector<set<int>>v(55);
for(int i = 1; i <= n; i ++)
{
int m; cin >> m;
a[i] = m;
while(m --)
{
int x;
cin >> x;
// cout << m << endl;
v[i].insert(x);
}
}
int k; cin >> k;
while(k --)
{
int x, y;
cin >> x >> y;
set<int>::iterator it;
//set用迭代器进行遍历,所以遍历的都是地址,需解引用才可得到值
double nc = 0;
for(it = v[x].begin(); it != v[x].end(); it ++)\
{
if(v[y].find(*it) != v[y].end()) //没找到时返回 .end()
nc ++;
}
double nt = v[x].size() + v[y].size() - nc;
cout << fixed << setprecision(2);
cout << nc/nt*100 << "%" << endl;
}
stl交集和并集
while(k --)
{
int x, y;
cin >> x >> y;
vector<int>nc, nt;
set_intersection(v[x].begin(), v[x].end(), v[y].begin(), v[y].end(), inserter(nc, nc.begin()));
nc.erase(unique(nc.begin(), nc.end()), nc.end());
set_union(v[x].begin(), v[x].end(), v[y].begin(), v[y].end(), inserter(nt, nt.begin()));
nt.erase(unique(nt.begin(), nt.end()), nt.end());
cout << fixed << setprecision(2);
cout << double(nc.size()) / double(nt.size())* 100.0 << "%" << endl;
}
set用法详解 https://blog.csdn.net/xiao__hei__hei/article/details/87350919?ops_request_misc=&request_id=&biz_id=102&utm_term=set%3Cint%3E::iterator%20it&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-1-87350919.142^v99^control&spm=1018.2226.3001.4187
C++ STL 求并集、交集、差集
https://blog.csdn.net/weixin_43889841/article/details/104098269