Greedy Gift Givers

Source: Internet
Author: User

Original title Address

Greedy Gift Givers

A Group of NP (2≤NP≤10) uniquely named friends have decided to exchange gifts of money. Each of the these friends might or might is give some money to any or all of the other friends. Likewise, each friend might or might is not a receive money from any or all of the other friends. Your goal in this problem are to deduce how much + money each person gives than they receive.

The rules for gift-giving is potentially different than you might expect. Each person sets aside a certain amount the money to give and divides this money evenly among all those to whom he or she I s giving a gift. No fractional money are available, so dividing 3 among 2 friends would being 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 is more giving than others (or at least may has more acquaintances) and some people There are more money than others.

Given A group of friends, no one of the whom have a name longer than characters, the money each person in the group spends O n Gifts, and a (sub) list of friends to whom each person gives gifts, determine how much + (or less) the Group gives than they receive. IMPORTANT NOTE

The grader machine was a Linux machine, that uses, Unix conventions:end of Line was a single character often known a s ' \ n '. This differs from Windows, which ends lines with the charcters, ' \ n ' and ' \ R '. Your program get trapped by this! Program NAME:GIFT1 INPUT FORMAT

Line 1: The single integer, NP
Lines 2..np+1: Each line contains the name of a group member
Lines Np+2..end: NP groups of lines organized like this:
The first line of the group tells the person ' s name, who'll be giving gifts.
The second line in the group contains, numbers:the initial amount of money (in the range 0..2000) to being divided up int o Gifts by the giver and then the number of people to whom the giver would give gifts, NGi (0≤ngi≤np-1).
If NGi is nonzero, each of the next NGi lines lists the name of a recipient of a gift.

SAMPLE INPUT (file gift1.in)

5
Dave
Laura
Owen
Vick
Amr
Dave
3
Laura
Owen
Vick
Owen
1
Dave
amr
2
Vick
Owen
Laura
0 2
Amr
Vick
Vick
0 0
OUTPUT FORMAT

The output is NP-lines, each with the name of a person followed by a single blank followed by the net gain or loss (Final_ Money_value-initial_money_value) for the person. The names should is printed in the same order they appear on line 2 of the input.

All gifts is integers. Each person gives the same integer amount of money to all friend to whom all money is given, and gives as much as Possibl E that meets this constraint. Given is kept by the giver. SAMPLE OUTPUT (file gift1.out)

Dave 302
Laura
owen-359
Vick 141
amr-150
/*
id:liuming9
lang:c++
task:gift1 */
#include <cstdio>
#include <cstring>

int N, T, a, B;
Char tmp[15], f[15];
struct e{
	char name[15];
	int money;
} BUF[11];

Find where the name is located
int find (char *p) {for
	(int i = 1; I <= n; i++)
		if (!strcmp (p, buf[i].name))
			return i;
  }

int main () {
	freopen ("gift1.in", "R", stdin);
	Freopen ("Gift1.out", "w", stdout);
	Read-in number
	scanf ("%d", &n);
	Read in the name of the owner for
	(int i = 1; I <= n; i++) {
		Buf[i].money = 0;
		scanf ("%s", buf[i].name);
	}
	while (~SCANF ("%s", tmp)) {
		scanf ("%d%d", &a, &b);
		if (!b)
			continue;
		int av = A/b;
		Buf[find (tmp)].money-= av * b;//distribution gift Money-av * b;
		Distributed gift to A/b number of money
		while (b--) {
			scanf ("%s", f);
			Buf[find (f)].money + = av
		;
	}
	for (int i = 1; I <= n; i++)
		printf ("%s%d\n", Buf[i].name, Buf[i].money);
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.