Greedy Gift Givers
A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchangegifts of money. Each of these friends might or might not give some money to anyor all of the other friends. Likewise, each friend might or might not receivemoney from any or all of the other friends. Your goal in this problem is todeduce how much more money each person gives than they receive.
The rules for gift-giving are potentially different than you might expect.Each person sets aside a certain amount of money to give and divides this moneyevenly among all those to whom he or she is giving a gift. No fractional moneyis available, so dividing 3 among 2 friends would be 1 each for the friendswith 1 left over -- that 1 left over stays in the giver's "account".
In any group of friends, some people are more giving than others (or atleast may have more acquaintances) and some people have more money than others.
Given a group of friends, no one of whom has a name longer than 14characters, the money each person in the group spends on gifts, and a (sub)listof friends to whom each person gives gifts, determine how much more (or less)each person in the group gives than they receive.
IMPORTANT NOTE
The grader machine is a Linux machine that uses standard Unix conventions:end of line is a single character often known as '\n'. This differs fromWindows, which ends lines with two charcters, '\n' and '\r'. Do not let yourprogram get trapped by this!
PROGRAM NAME: gift1
INPUT FORMAT
Line 1: | The single integer, NP | |||
Lines 2..NP+1: | Each line contains the name of a group member | |||
Lines NP+2..end: | NP groups of lines organized like this:
|
SAMPLE INPUT (file gift1.in)
5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0
OUTPUT FORMAT
The output is NP lines, each with the name of a person followed by asingle blank followed by the net gain or loss (final_money_value - initial_money_value)for that person. The names should be printed in the same order they appear online 2 of the input.
All gifts are integers. Each person gives the same integer amount of moneyto each friend to whom any money is given, and gives as much as possible thatmeets this constraint. Any money not given is kept by the giver.
SAMPLE OUTPUT (file gift1.out)
dave 302
laura 66
owen -359
vick 141
amr -150
题目大意是:有一群人要互相送礼物,嗯,没错,送的是钱。每个人要给几个人送礼物,每份礼物的分量(钱数)就是他们手上的钱数除以要分的份数向下取整,剩余的仍归他自己所有(注意:这里的所谓“手上有的钱”不包括他前面的人送给他的钱)。程序的任务是在第一轮送完钱后,算出每个人比原来多多少钱(比原来少的话用负数表示)。
输入格式:
第一行一个数字np,表示这堆人有np个。
接下来有np行,每行一个人名,表示这些人的名字························1
接下来有np组数据:
每组数据的第一行是一个人名,代表要送礼物的人。························2 第二行是两个数字n和m,表示这人手上有n块钱,要分给m个人。
接下来m行是2里面这个人要送礼物的人。
输出格式:
输出np行,每行由一个字符串和一个数字构成,字符串表示人名,数字表示他比原来多了多少钱。(注意:人名的顺序是按1里面的顺序输出的)。
数据范围:2≤np≤10,人名长度不大于14
第一层的题目无需多提,模拟就好,反正楼主看到这题第一反应是不用stl都对不起C++了。
/*
ID:su1q2d21
LANG:C++
TASK:gift1
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<map>
#define maxn 20
using namespace std;
int main()
{
freopen("gift1.in","r",stdin);
freopen("gift1.out","w",stdout);
int np;
int i,j;
int mon;
int diver;
string name[maxn];
map<string,int> get;
string giver,receiver;
map<string,int> account;
cin>>np;
for(i=1;i<=np;i++){
cin>>name[i];
get[name[i]]=0;
}
for(i=1;i<=np;i++){
cin>>giver;
cin>>mon;
cin>>diver;
account[giver]=mon;
if(diver!=0) mon/=diver;
get[giver]+=(account[giver]-mon*diver);
for(j=1;j<=diver;j++){
cin>>receiver;
get[receiver]+=mon;
}
}
for(i=1;i<=np;i++) cout<<name[i]<<' '<<(get[name[i]]-account[name[i]])<<endl;
return 0;
}