题目描述:
A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money. Each of these friends might or might not give some money to some or all of the other friends (although some might be cheap and give to no one). Likewise, each friend might or might not receive money from any or all of the other friends. Your goal is to deduce how much more money each person receives than they give.
The rules for gift-giving are potentially different than you might expect. Each person goes to the bank (or any other source of money) to get a certain amount of money to give and divides this money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 7 among 2 friends would be 3 each for the friends with 1 left over – that 1 left over goes into the giver's "account". All the participants' gift accounts start at 0 and are decreased by money given and increased by money received.
In any group of friends, some people are more giving than others (or at least 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 14 characters,
- the money each person in the group spends on gifts, and
- a (sub)list of friends to whom each person gives gifts,
determine how much money each person ends up with.
输入输出要求:
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
SAMPLE OUTPUT (file gift1.out)
dave 302 laura 66 owen -359 vick 141 amr -150
题目大意:
共有NP个人,NP个人轮流送礼物。轮流输入要送礼物的人的名字S,输入拥有的钱数M与分的人数N,再依次输入要分给对象的人名(一个N个),分完后分钱人剩下钱数(S%N),被分钱对象分得的钱数为平均数(S/N),算出最后每人剩余的钱数。NP范围为2到10,钱数M是0到2000。
解题思路:
本题难点在于按格式要求读入数据,处理其实很简单,用一个字典来存储,字典的键是送礼物者的姓名,值是每一次送出礼物或者接收到礼物自己余额的变化,最后就是按格式输出,一定要注意就是usaco文件结尾使用\n的,哎,因为这个问题自己被坑了几次了
python解题代码:
"""
ID: scwswx2
LANG: PYTHON3
TASK: gift1
"""
fout = open ('gift1.out', 'w')
with open ('gift1.in', 'r') as fin:
x=int(fin.readline().strip('\n'))
name={}
for i in range(x):
username=fin.readline()
name[username.strip('\n')]=0
#print(name)
while True:
y=fin.readline().strip('\n')#读入名字
if not y:
break
a,b = map(int, fin.readline().split())#读入原来钱和分配人数
if b!=0:
temp=int(a/b)#记录每一个人分得的钱
name[y]=name.get(y)-(temp*b)
for i in range(b):
tempname=fin.readline().strip('\n')
name[tempname]=name.get(tempname)+temp
else:
name[y]=name.get(y)+0
#print(name)
#count=0
for i in name:
#print(i+' '+str(name[i]))
fout.write(i+' '+str(name[i])+'\n')
fout.close()