# 程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

:: :: :: :: :: :: :: ::
 77 随笔 :: 0 文章 :: 442 评论 :: 0 引用

Time limit: 3.000 seconds

## The Problem问题

This problem involves determining, for a group of gift-giving friends, how much more each person gives than they receive (and vice versa for those that view gift-giving with cynicism).

In this problem each person sets aside some money for gift-giving and divides this money evenly among all those to whom gifts are given.

However, 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, the money each person in the group spends on gifts, and a (sub)list of friends to whom each person gives gifts; you are to write a program that determines how much more (or less) each person in the group gives than they receive.

## The Input输入

The input is a sequence of gift-giving groups. A group consists of several lines:

• the number of people in the group,
圈子中的人数，
• a list of the names of each person in the group,
圈子中每个人的姓名，
• a line for each person in the group consisting of the name of the person, the amount of money spent on gifts, the number of people to whom gifts are given, and the names of those to whom gifts are given.
每个人独占一行，包括姓名、用于购礼的总金额、要送礼的人数以及每个要送的人的名字。

All names are lower-case letters, there are no more than 10 people in a group, and no name is more than 12 characters in length. Money is a non-negative integer less than 2000.

The input consists of one or more groups and is terminated by end-of-file.

## The Output输出

For each group of gift-givers, the name of each person in the group should be printed on a line followed by the net gain (or loss) received (or spent) by the person. Names in a group should be printed in the same order in which they first appear in the input.

The output for each group should be separated from other groups by a blank line. All gifts are integers. Each person gives the same integer amount of money to each friend to whom any money is given, and gives as much as possible. Any money not given is kept and is part of a person's "net worth" printed in the output.

## Sample Input输入示例

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
3
liz steve dave
liz 30 1 steve
steve 55 2 liz dave
dave 0 2 steve liz

dave 302
laura 66
owen -359
vick 141
amr -150

liz -3
steve -24
dave 27

## Solution解答

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
//主函数
int main(void) {
//循环处理输入的每组朋友圈
for (int nPeople, nFirst = 1; cin >> nPeople; nFirst = 0) {
//vecName按顺序存储各人姓名，vecWorth为各人净收入
vector<string> vecName(nPeople);
vector<int> vecWorth(nPeople, 0);
//输入所有人的姓名
for (int i = 0; i < nPeople; cin >> vecName[i++]);
//循环处理每个人的收支
for (int i = 0, nMoney, nFriCnt; i < nPeople; ++i) {
//读入姓名，金额和朋友数
string strName;
cin >> strName >> nMoney >> nFriCnt;
//找到此人在数组中的位置
vector<string>::iterator iPeople = find(
vecName.begin(), vecName.end(), strName);
int &nCurWorth = vecWorth[distance(vecName.begin(), iPeople)];
//从此人净收入中支出金额分平均分给朋友
nCurWorth -= nMoney;
if (nFriCnt > 0) {
//平分到每个朋友的净收入中
for (int j = 0, nShare = nMoney / nFriCnt; j < nFriCnt; ++j) {
//找到朋友在数组中的位置，并加入到净收入中
cin >> strName;
iPeople = find(vecName.begin(), vecName.end(), strName);
vecWorth[distance(vecName.begin(), iPeople)] += nShare;
}
//计算平分后剩下的零头
nMoney %= nFriCnt;
}
//如果剩有零头或未分给任何朋友，则返回净收入
nCurWorth += nMoney;
}
//按照要求，每组输出间要以空行隔开，但第一组前面不能有空行
cout << (nFirst == 1 ? "" : "\n");
//按顺序输出结果，每行前面是姓名，后面是净收入，用空格隔开
for (int i = 0; i != vecName.size(); ++i) {
cout << vecName[i] << ' ' << vecWorth[i] << endl;
}
}
return 0;
}


posted on 2010-08-14 18:18  Devymex  阅读(...)  评论(...编辑  收藏