ArticleDirectory
- Important Note
- Program name: gift1
- Input Format
- Sample input (File gift1.in)
- Output Format
- Sample output (File gift1.out)
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 CT. 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 wocould 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 when Ds 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.
Important Note
The grader machine is a Linux machine that uses standard UNIX conventions: End of line is a single character often known as '\ n '. this differs from windows, which ends lines with two charcters, '\ n' and' \ R '. do not let your program get trapped by this!
Program name: gift1input 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 in the group tells the person's name who will be giving gifts. |
The second line in the group contains two numbers: the initial amount of money (in the range 0 .. 2000) to be divided up into gifts by the giver and then the number of people to whom the giver will 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)
5davelauraowenvickamrdave200 3lauraowenvickowen500 1daveamr150 2vickowenlaura0 2amrvickvick0 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 that person. the names shoshould be printed in the same order they appear on line 2 of the input.
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 that meets this constraint. any money not given is kept by the giver.
Sample output (File gift1.out)
Dave 302 Laura 66 Owen-359 Vick 141amr-150
View code /*
ID: lxlenovos1
Prog: gift1
Lang: C ++
*/
# Include < Iostream >
# Include < Fstream >
# Include < String >
# Include < Vector >
# Include < String . H >
Using Namespace STD;
Int Main ( Void )
{
Ofstream fout ( " Gift1.out " );
Ifstream fin ( " Gift1.in " );
Vector < String > Vgift;
Int NP, I, j, Z;
Fin > NP;
// Cin> NP;
Int G [NP];
For (I = 0 ; I < NP; I ++ )
{
String S;
// Cin> S;
Fin > S;
Vgift. push_back (s );
G [I] = 0 ;
}
// Memset (G, 0, NP );
For (I = 0 ; I < NP; I ++ )
{
String STR;
Vector < String > Substr;
Int Temp;
Fin > STR;
// Cin> STR;
Int TG, D;
Fin > TG > D;
For (Z = 0 ; Z < NP; Z ++ )
{
If (Str = Vgift [Z])
{
If (D ! = 0 )
G [Z] + = (TG % D) - TG;
Else
G [Z] -= TG;
Break ;
}
}
If (D > 0 )
{
Temp = TG / D;
For (J = 0 ; J < D; j ++ )
{
String S;
Fin > S;
For (Z = 0 ; Z < NP; Z ++ )
{
If (S = Vgift [Z])
{
G [Z] + = Temp;
}
}
}
}
}
For (I = 0 ; I < NP; I ++ )
{
Fout < Vgift [I] < " " < G [I] < Endl;
}
}