Greedy Gift Givers


/*
ID: tausiq11
PROG: gift1
LANG: C++
*/

#include <stdio.h>
#include <string.h>

struct friends {
    int spend;
    int receive;
    char name [20];
} A [12] = {0, 0, 0};

int main ()
{
    freopen ("gift1.in", "r", stdin);
	freopen ("gift1.out", "w", stdout);

	long np, i, money, person, divide, c;
    char giver [20], receiver [20];

    scanf ("%ld", &np);

    for (i = 0; i < np; i++)
    scanf ("%s", A [i].name);

    for (c = 0; c < np; c++ ) {

        scanf ("%s", giver);
        scanf ("%ld", &money);

        i = 0;
        while (strcmp (A [i].name, giver) != 0)
            i++;
        A [i].spend += money;

        scanf ("%ld", &person);
		if (person == 0) {
			A [i].receive += money;
			continue;
		}

        divide = person;
        A [i].receive += money - ((money / person) * person);
        
        while (person--) {
            scanf ("%s", receiver);
            i = 0;
            while (strcmp (A [i].name, receiver) != 0)
                i++;
            A [i].receive += (money / divide);
        }
    }
    
    for (i = 0; i < np; i++)
		printf("%s %ld\n", A [i].name, A [i].receive - A [i].spend);
	
	return 0;
}

One thought on “Greedy Gift Givers

  1. Does this program work? It seems to me that your while loops are not going to work.
    while (strcmp (A [i].name, giver) != 0)
    i++;
    This one especially…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s