USACO 1.1贪婪的送礼者题解

又过了一道大水题。这道题目并不是特别难,如果用了map结构就很简单了。但是有几个地方比较坑。

比如说这道题目说要输出收到的钱与送出钱之差,而送出的钱要向下取整平均分

这样他预先准备的钱不一定会送出。所以你懂得

还有一个地方就是有可能这个人不送给别人钱,那么这种情况就要continue掉,要不然求平均数时会因分母变0而报错

最后我们来讲一下map用法:

首先我们要用map头文件

#include <map>

然后我们要建立map变量

map <string,int> pep;

接下来我们就可以这么用

map[lx]=25;

好了,废话不说贴上代码

#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
#include <cstring>

using namespace std;

int main(){
    
    int num_pep;
    cin>>num_pep;
    
    string name[25];
    map <string,int> wallet_pep;
    map <string,int> wallet_recive;//Every one's account which uses to recive money.
    
    //chu shi hua ge ren zhang hu
    string temp_name;
    for (int i=1;i<=num_pep;i++){
        cin>>temp_name;
        name[i]=temp_name;
        wallet_pep[temp_name]=0;
        wallet_recive[temp_name]=0;
    }
    
    string name_send,temp_name_recive;//The name of people who send gifts.
    int temp_money,temp_num_recive,k;
    for (int i=1;i<=num_pep;i++){
        cin>>name_send;
        cin>>temp_money>>temp_num_recive;
        if (temp_num_recive==0) continue;
        k=temp_money/temp_num_recive;
        wallet_pep[name_send]=k*temp_num_recive;
        for (int j=1;j<=temp_num_recive;j++){
            cin>>temp_name_recive;
            wallet_recive[temp_name_recive]+=k;
        }
    }
    
    for (int i=1;i<=num_pep;i++){
        cout<<name[i]<<" "<<wallet_recive[name[i]]-wallet_pep[name[i]]<<endl;
    }
    
    return 0;
    
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值