滨州做网站建设价格杭州网站外包
题目描述
公司开展了一次相亲派对,若男生的颜值和女生的颜值相同,则可以进行配对。
现在给出所有男生的颜值,以及所有女生的颜值,请你计算可以产生多少种配对。
输入描述
第一行输入男生颜值数组。数组长度不大于10000。
第二行输入女生颜值数组。数组长度不大于10000。
注意:颜值最大不超过100000
输出描述
输出可以产生多少种配对。
用例
输入
[1, 2, 2, 3, 3, 3, 4, 4]
[3, 3, 4, 5, 5]
Copy
输出
8
Copy
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{map<int, int> mp1, mp2;string str;getline(cin, str);str = str.substr(1, str.size() - 2); // 去掉两端的方括号size_t pos = 0;while ((pos = str.find(',')) != string::npos){int x = stoi(str.substr(0, pos));mp1[x]++;// 统计该颜值男生出现次数str.erase(0, pos + 1); // 删除处理过的部分}mp1[stoi(str)]++;//输入处理同上getline(cin, str);str = str.substr(1, str.size() - 2);pos = 0;while ((pos = str.find(',')) != string::npos){int x = stoi(str.substr(0, pos));mp2[x]++;str.erase(0, pos + 1);}mp2[stoi(str)]++;int res = 0;for (auto x : mp1) // 遍历男生颜值{res += x.second * mp2[x.first]; // 计算男生和女生中相同颜值的配对数量}cout << res << endl; // 输出配对总数}signed main()
{solve();return 0;
}