My second Usaco training--greedy Gift givers

Source: Internet
Author: User
Tags assert readline

Topic:

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


The key is to pay attention to ReadLine () when read, the first line gives a total number of individuals, that is, NP personal

The next NP line is the name of the NP individual, and then comes down to the name of the person who is going to give money to someone else, down is money and the number of people who want to send money (numberpeople). The next numberpoeple line is the name of the person who gets the money. Later, and so on, use the For statement

My answer:

/* id:yang4521 Lang:java TASK:GIFT1 */import java.io.*;
Import java.util.*; Class Gift1 {public static void main (string[] args) throws IOException {BufferedReader f = new BufferedReader                                   
	    (New FileReader ("gift1.in"));
	    PrintWriter out = new PrintWriter (new BufferedWriter (New FileWriter ("Gift1.out"));
	    int NP = Integer.parseint (F.readline ());
	    
	    arraylist<string> listname = new arraylist<string> ();
	    String name= "";
	    	for (int. i=0;i<np;i++)//get the all name of giver {name=f.readline ();
	    Listname.add (name);  } int[] Initialmoney = new Int[np];//initial_money_value int[] Finalmoney = new INT[NP];
			Final_money_value for (int i=0;i<np;i++) {name=f.readline ();
			String moneyandnumber= "";
			Moneyandnumber=f.readline ();//get the money and the number of people string[] STRs =moneyandnumber.split ("");
			int Money=integer.parseint (strs[0]); int Numberpeople=integer.parseint (Strs[1]);
			int average=0;
		    int leftover=0;
		    	 if (numberpeople!=0) {average=money/numberpeople;
			Leftover=money%numberpeople; } for (int j = 0;j<listname.size (); j + +)//{if (Listname.get (j). Equals (name))//giver name {init
				  Ialmoney[j]=money;
			  Finalmoney[j]+=leftover;
				}} for (int k=0;k<numberpeople;k++)//give The People {name=f.readline (); for (int JJ = 0;jj<listname.size (); jj++) {if (Listname.get (JJ). Equals (name))//{finalmoney[jj]+=
				  Average
			}}}} for (int i=0;i<np;i++) {out.print (Listname.get (i) + "");
		Out.println (Finalmoney[i]-initialmoney[i]);  } out.close ();
	  Close the output file System.exit (0);
 }

}


The official answer: analysis:
Greedy Gift givers
Russ Cox

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.