UVA119 Greedy Gift Givers【map+水题】

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.
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.
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
Sample Output
dave 302
laura 66
owen -359
vick 141
amr -150
liz -3
steve -24
dave 27

问题链接:UVA119 Greedy Gift Givers
问题简述:(略)
问题分析
    n个人之间互送礼物,给定这n个人的名字,以及送礼人姓名、钱数、份数、收礼人姓名,计算每个人的盈亏。给出的钱数要平分,剩下的零头(余数)留给自己。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA119 Greedy Gift Givers */

#include 

using namespace std;

const int N = 10;
string name[N];

int main()
{
    int n, k = 0;
    while(cin >> n) {
        map<string, int> m;
        for(int i = 0; i < n; i++)
            cin >> name[i];

        string s1, s2;
        int a, b;
        for(int i = 1; i <= n; i++) {
            cin >> s1 >> a >> b;
            for(int j = 1; j <= b; j++) {
                cin >> s2;
                if(a != 0 && b != 0)
                    m[s2] += a / b;
            }
            if(a != 0 && b != 0) {
                m[s1] += a % b;
                m[s1] -= a;
            }
        }

        if(k++) cout << endl;
        for(int i = 0; i < n; i++)
            cout << name[i] << " " << m[name[i]] << endl;
    }

    return 0;
}

你可能感兴趣的:(#,ICPC-备用二,#,ICPC-STL标准模板库)