1065 单身狗 (25 分)(map+set+vector)


一.题目

在这里插入图片描述


二.分析

在这里,我将完整的代码分为四个部分:

∙ \bullet 第一部分:使用 u n o r d e r e d _ m a p < i n t , i n t > m p unordered \_map<int,int>mp unordered_map<int,int>mp 存储各对夫妻的 I D ID ID,使用键值对使其一一对应。

∙ \bullet 第二部分:使用 s e t set set 存储接下来输入的客人 I D ID ID,方便搜索信息。

∙ \bullet 第三部分:使用 v e c t o r vector vector 存储落单客人的 I D ID ID。首先如果客人 I D ID ID 没有出现在 m p mp mp 中,那么他一定是落单的;然后如果他出现在 m p mp mp 中,但是他的配偶没有来到现场(不在 s e t set set 中),那么也是落单的。

∙ \bullet 第四部分:将数组排序并输出结果。注意输出结果一定是五位数。


三.代码

#include<iostream>
#include<vector>
#include<unordered_map>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    int N=0,M=0;
    cin>>N;
    
    //加入map
    unordered_map<int,int>mp;
    for(int i=0;i<N;i++)
    {
        int a=0,b=0;
        cin>>a>>b;
        mp[a]=b;
        mp[b]=a;
    }
    
    //加入M个客人ID
    cin>>M;
    set<int>s;
    for(int i=0;i<M;i++)
    {
        int a=0;
        cin>>a;
        s.insert(a);
    }
    
    //找出落单客人
    vector<int>res;
    for(auto &a:s)
    {
        if(!mp.count(a))
        {
            res.emplace_back(a);
        }else
        {
            if(s.find(mp[a])!=s.end()) continue;
            else res.emplace_back(a);
        }
    }
    
    //排序后输出
    sort(res.begin(),res.end());
    cout<<res.size()<<endl;
    for(int i=0;i<res.size();i++)
    {
        printf("%05d", res[i]);
        if(i!=res.size()-1) cout<<" ";
    }
    return 0;
}

四.结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比奇堡咻飞兜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值