这道题大意就是几个人互送礼物,让你求每个人的盈利。
原题给的样例数据:
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
这题使用模拟算法就行了,就是注意输入的时候不要掉坑里了。
还有就是比如说自己有200快钱给3个人,每个人得到66块,剩下两块自己收,并计入答案。
还有就是想题目里的vick,分给0个人,千万不要除以了0。。
下面是我的程序:
/* ID:aaabbbr1 LANG:C++ TASK:gift1 */ #include <stdio.h> #include <string> #include <map> #include <iostream> using namespace std; struct person { string name; int gave[10],have,cnt,receive; person(){have=receive=0;} }a[10]; map<string,int>link;//这只是方便从人名找到编号而已 void gavemoney(int x) { if(a[x].cnt==0)return;//小心除以0了(也就是如果给的人数为0就不给了) int per=a[x].have/a[x].cnt;//计算每人多少钱 int tot=per*a[x].cnt;//计算一共给多少钱 a[x].receive+=a[x].have-tot;//计算自己的零钱 并汇入收到的钱里面 for(int i=0;i<a[x].cnt;i++)//给钱 a[ a[x].gave[i] ].receive += per; } void display(int x) { cout << a[x].name << " " << a[x].receive-a[x].have << "\n"; } int main() { freopen("gift1.in","r",stdin); freopen("gift1.out","w",stdout); string tmp; int n,x,t1,t2; cin >> n; for(int i=0;i<n;i++) { cin >> tmp; a[i].name=tmp; link[tmp]=i; } for(int i=0;i<n;i++) { cin >> tmp; x=link[tmp]; cin >> t1 >> t2; a[x].have=t1; a[x].cnt=t2; for(int j=0;j<a[x].cnt;j++) { cin >> tmp; a[x].gave[j]=link[tmp]; } } for(int i=0;i<n;i++) gavemoney(i); for(int i=0;i<n;i++) display(i); fclose(stdin); fclose(stdout); return 0; }
======================
原题中文翻译(Collected by BirDOR):
★Greedy Gift Givers 贪婪的礼物送礼者
对于一群要互送礼物的朋友,你要确定每个人送出的礼物比收到的多多少(and vice versa for
those who view gift giving with cynicism).
在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物
的人.
然而,在任何一群朋友中,有些人将送出较多的礼物(可能是因为有较多的朋友),有些人有准备了较
多的钱.
给出一群朋友, 没有人的名字会长于 14 字符,给出每个人将花在送礼上的钱,和将收到他的礼物
的人的列表,
请确定每个人收到的比送出的钱多的数目.
IMPORTANT NOTE
测试系统是 Linux 符合标准的 Unix 的协定.
用‘\n‘作为行的结束.
这和 Windows 系统用‘\n‘ 和 ‘\r‘作为行的结束是不同的.
你的程序不要被这困住了.
PROGRAM NAME: gift1
INPUT FORMAT
第 1 行:人数NP,2<= NP<=10
第 2 到 NP+1 行:这NP 个在组里人的名字 一个名字一行
第NP+2 到最后:这里的NP 段内容是这样组织的:
第一行是将会送出礼物人的名字.
第二行包含二个数字: 第一个是原有的钱的数目(在0 到2000 的范围里),第二个NGi 是将收到
这个送礼者礼物的人的个数 如果 NGi 是非零的, 在下面 NGi 行列出礼物的接受者的名字,一个
名字一行.
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
输出 NP 行
每行是一个的名字加上空格再加上收到的比送出的钱多的数目.
对于每一个人,他名字的打印顺序应和他在输入的2 到NP+1 行中输入的顺序相同.所有的送礼的钱
都是整数.
每个人把相同数目的钱给每位要送礼的朋友,而且尽可能多给,不能给出的钱被送礼者自己保留.
SAMPLE OUTPUT (file gift1.out)
dave 302
laura 66
owen -359
vick 141
amr -150