E1-相亲派对(组合)

题目描述

公司开展了一次相亲派对,若男生的颜值和女生的颜值相同,则可以进行配对。

现在给出所有男生的颜值,以及所有女生的颜值,请你计算可以产生多少种配对。

输入描述

第一行输入男生颜值数组。数组长度不大于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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值