UVA Greedy Gift Givers

Source: Internet
Author: User
Tags assert
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
Analysis: This topic should be aware that the first input order is different from the second input order, and then the key is to find the function. Code:
#include <iostream> #include <fstream> #include <string> using namespace std;
    struct person{string name;
    int Inital_money;
    int Final_money;
    Char lucky_list[10];
    int lucky_num;
int Give_money;
}; Char getId (string* lucker,string &name) {for (char i=0;i<=10;i++) if (lucker[i]==name) retur
    n i;
return-1;
    } int main (int argc, const char * argv[]) {ofstream fout ("Gift1.out");
    Ifstream fin ("gift1.in");
    int PN;
    String luck_list[10];
    Person P[10];
        while (fin>>pn&&pn>=2&&pn<=10) {string str;
        for (int i=0;i<pn;i++) fin>>luck_list[i];
            for (int ip=0;ip<pn;ip++) {fin>>str;
            Char Pos=getid (luck_list, str);
            P[POS].NAME=STR;
            fin>>p[pos].inital_money>>p[pos].lucky_num; if (p[pos].lucky_num) p[pos].give_money=p[pos].inital_money/p[pos].luCky_num;

                else p[pos].give_money=0;
            P[pos].final_money=p[pos].inital_money-p[pos].give_money*p[pos].lucky_num;
        for (int io=0;io<p[pos].lucky_num;io++) {Fin>>str;p[pos].lucky_list[io]=getid (luck_list, str);}
                } for (int i=0;i<pn;i++) {for (int j=0;j<p[i].lucky_num;j++) {
            P[p[i].lucky_list[j]].final_money + = P[i].give_money; }} for (int i=0;i<pn;i++) {fout<<p[i].name<< "" <<p[i].final_money-p[i].inital
    _money<<endl;}
} return 0;
 }
Operation Result:
User:jim Zhai [jszhais1]
task:gift1
lang:c++

compiling ...
Compile:ok

Executing
   ... Test 1:test OK [0.000 secs, 3360 KB]
   test 2:test OK [0.000 secs, 3360 KB]
   test 3:test OK [0.000 secs, 3360 kb]<  C8/>test 4:test OK [0.000 secs, 3360 KB]
   test 5:test OK [0.000 secs, 3360 KB]
   test 6:test OK [0.000 secs, 3360 KB]
   Test 7:test OK [0.000 secs, 3360 KB]
   test 8:test OK [0.000 secs, 3360 KB]
   test 9:test OK [0.000 sec S, 3360 KB] all

tests OK.

Your program (' GIFT1 ') produced all correct answers! This is your submission #2 the for this problem. congratulations!

The answer:

The hardest part on this problem are dealing with the strings representing people ' s names.

We keep An array of the person structures this contain their name and how much money they give/get.

The heart of the program are the lookup () function, given a person's name, returns their person structure. We Add new people with Addperson ().

Note that we assume names is reasonably short.

#include <stdio.h> #include <string.h> #include <assert.h> #define Maxpeople #define Namelen Typ
edef struct person person;
    struct person {char Name[namelen];
int total;

};
Person People[maxpeople];

int npeople;
	void Addperson (char *name) {assert (Npeople < maxpeople);
    strcpy (people[npeople].name, name);
npeople++;

    } person* Lookup (char *name) {int i; /* Look for name in people table */for (i=0; i<npeople; i++) if (strcmp (name, people[i].name) = = 0) return &am

    P;people[i];	ASSERT (0);
    /* should have found name */} int main (void) {char name[namelen];
    FILE *fin, *fout;
    int I, J, NP, AMT, NG;

    Person *giver, *receiver;
    Fin = fopen ("gift1.in", "R");

    Fout = fopen ("Gift1.out", "w");
    FSCANF (FIN, "%d", &AMP;NP);

    ASSERT (NP <= maxpeople);
	for (i=0; i<np; i++) {fscanf (Fin, "%s", name);
    Addperson (name); }/* Process gift lines */for (i=0; i<np; i++) {fscanf (fIn, "%s%d%d", name, &amt, &ng);

	Giver = lookup (name);
	    for (j=0; j<ng; J + +) {fscanf (Fin, "%s", name);
	    Receiver = lookup (name);
	    Giver->total-= Amt/ng;
	Receiver->total + = Amt/ng; }}/* Print gift Totals */for (i=0; i<np; i++) fprintf (Fout, "%s%d\n", People[i].name, People[i].total)
    ;
Exit (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.