又过了一道大水题。这道题目并不是特别难,如果用了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;
}