原题:
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 any or all of the other friends. Likewise, each friend might or might not receive money from any or all of the other friends. Your goal in this problem is to deduce 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 money evenly among all those to whom he or she is giving a gift. No fractional money is available, so dividing 3 among 2 friends would be 1 each for the friends with 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 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 more (or less) each person in the group gives than they receive.
原题大意及解析:有np<=10个人,每个人本身有一定的钱数和一定的朋友数。
每个人向自己的朋友送钱,送钱的规则是有的钱整除朋友个数分给每个朋友,余下的钱自己保留。
要求的结果是:每个人得到的钱 - 自己送出去的钱。
输入格式:
line 1:人数np
line 2~np+1:人名
下面np组数据:
人名
money 朋友数
朋友名
朋友名
。
。
money 朋友数
朋友名
朋友名
。
。
输出格式:
人名 每个人得到的钱 - 自己送出去的钱
人名 每个人得到的钱 - 自己送出去的钱
。
。
样例输入输出:
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
代码如下:
/* ID:newyear111 PROG: gift1 LANG: C++ *///模拟题 #include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=15;
//定义结构体保存每个的姓名,拥有的钱,朋友数,朋友的编号
struct person{string name;int money;int f_Num;int f_ID[N];
}p[N];
//全局变量 n代表人数,ans[N]保存对应编号的人:获得的钱-给出的钱
int n,ans[N];
//返回对应姓名的编号
int find_ID(string name)
{for(int j=0;j<n;j++)if(name==p[j].name)return j;
}int main()
{ifstream fin("gift1.in");ofstream fout("gift1.out"); //输入部分 fin>>n;for(int i=0;i<n;i++)fin>>p[i].name;for(int i=0;i<n;i++){string tmp1,tmp2;int t1,t2;fin>>tmp1;t1=find_ID(tmp1);fin>>p[t1].money>>p[t1].f_Num;for(int j=0;j<p[t1].f_Num;j++){fin>>tmp2;t2=find_ID(tmp2);p[t1].f_ID[j]=t2; }}//计算部分 for(int i=0;i<n;i++){if(!p[i].f_Num)continue;int t=p[i].money/p[i].f_Num;for(int j=0;j<p[i].f_Num;j++){ans[p[i].f_ID[j]]+=t;}ans[i]-=p[i].f_Num*t;}//输出部分 for(int i=0;i<n;i++)fout<<p[i].name<<" "<<ans[i]<<endl;fin.close();fout.close();return 0;
}