zoukankan      html  css  js  c++  java
  • Greedy Gift Givers

    Greedy Gift Givers

    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 expect. 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 would 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 spends 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: 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 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 the name of a recipient of a gift.

    SAMPLE INPUT (file gift1.in)

    5
    dave
    laura
    owen
    vick
    amr
    dave
    200 3
    laura
    owen
    vick
    owen
    500 1
    dave
    amr
    150 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 that person. The names should 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 141
    amr -150
    题解: n为人的名字个数
    n个名字
    接下来有n
    --------------------
    输入一个名字
    输入要给的钱数 和 人数
    要给的人的名字
    ---------------------
    OK code:
     1 /*
     2 ID: ******
     3 PROG: gift1
     4 LANG: C++
     5 */
     6 #include <iostream>
     7 #include <fstream>
     8 #include <string.h>
     9 using namespace std;
    10 typedef struct  
    11 {
    12     char name[20];
    13     int receive;
    14 }person;
    15 person P[1000];
    16 int find(char s[],int m)
    17 {
    18     int i;
    19     for(i=0;i<m;i++)
    20         if(strcmp(P[i].name,s)==0)
    21             break;
    22         return i;
    23 }
    24 
    25 int main() {
    26     ofstream fout ("gift1.out");
    27     ifstream fin ("gift1.in");
    28     
    29     char s[100];
    30     int m,n,t,i,v,g,f,j;
    31     fin>>m;
    32     for(i=0;i<m;i++)
    33     {
    34         fin>>P[i].name;
    35         P[i].receive=0;
    36     }
    37     for(i=0;i<m;i++)
    38     {
    39         fin>>s;    
    40         fin>>t>>v;
    41         n=find(s,m);
    42         P[n].receive-=t;
    43         if(t==0 && v==0) //要注意 0/0的情况,如果直接0/0 就会编译错误 
    44            f=0;
    45         else if(t%v==0 )
    46             f=t/v;
    47         else if(t%v!=0)
    48         {
    49             g=t-(t%v);
    50             f=g/v;
    51             P[n].receive+=(t%v);
    52         }
    53         for(j=0;j<v;j++)
    54         {
    55             fin>>s;
    56             n=find(s,m);
    57             P[n].receive+=f;
    58         }
    59     }
    60     for(i=0;i<m;i++)
    61         fout<<P[i].name<<" "<<P[i].receive<<endl;
    62     return 0;
    63 }

    官方代码:

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    
    #define MAXPEOPLE 10
    #define NAMELEN    32
    
    typedef 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 &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", &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);
    }

    ===================测试数据===============================

    ------- test 1 ----
    10
    
    mitnik
    
    Poulsen
    
    Tanner
    
    Stallman
    
    Ritchie
    
    Baran
    
    Spafford
    
    Farmer
    
    Venema
    
    Linus
    
    mitnik
    
    300 3
    
    Poulsen
    
    Tanner
    
    Baran
    
    Poulsen
    
    1000 1
    
    Tanner
    
    Spafford
    
    2000 9
    
    mitnik
    
    Poulsen
    
    Tanner
    
    Stallman
    
    Ritchie
    
    Baran
    
    Farmer
    
    Venema
    
    Linus
    
    Tanner
    
    1234 1
    
    Poulsen
    
    Stallman
    
    536 3
    
    Farmer
    
    Venema
    
    Linus
    
    Ritchie
    
    2000 1
    
    mitnik
    
    Baran
    
    79 2
    
    Tanner
    
    Farmer
    
    Farmer
    
    0 0
    
    Venema
    
    12 9
    
    mitnik
    
    Poulsen
    
    Tanner
    
    Stallman
    
    Ritchie
    
    Baran
    
    Spafford
    
    Farmer
    
    Linus
    
    Linus
    
    1000 1
    
    mitnik
    
    ------- test 2 ----
    5
    
    dave
    
    laura
    
    owen
    
    vick
    
    amr
    
    dave
    
    200 3
    
    laura
    
    owen
    
    vick
    
    owen
    
    500 1
    
    dave
    
    amr
    
    150 2
    
    vick
    
    owen
    
    laura
    
    0 2
    
    amr
    
    vick
    
    vick
    
    0 0
    
    ------- test 3 ----
    2
    
    john
    
    lennon
    
    lennon
    
    0 0
    
    john
    
    0 0
    
    ------- test 4 ----
    10
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Alex
    
    2000 9
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Bob
    
    2000 9
    
    Alex
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Catherine
    
    2000 9
    
    Alex
    
    Bob
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Dave
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Ebert
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Francis
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Godot
    
    Harris
    
    Iliya
    
    Jimbo
    
    Godot
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Harris
    
    Iliya
    
    Jimbo
    
    Harris
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Iliya
    
    Jimbo
    
    Iliya
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Jimbo
    
    Jimbo
    
    2000 9
    
    Alex
    
    Bob
    
    Catherine
    
    Dave
    
    Ebert
    
    Francis
    
    Godot
    
    Harris
    
    Iliya
    
    ------- test 5 ----
    4
    
    these
    
    names
    
    are
    
    dumb
    
    dumb
    
    534 3
    
    these
    
    dumb
    
    are
    
    are
    
    351 1
    
    names
    
    these
    
    509 2
    
    dumb
    
    names
    
    names
    
    278 1
    
    dumb
    
    ------- test 6 ----
    2
    
    someguy
    
    someotherguy
    
    someotherguy
    
    1500 1
    
    someguy
    
    someguy
    
    500 1
    
    someotherguy
    
    ------- test 7 ----
    8
    
    a
    
    b
    
    c
    
    d
    
    e
    
    f
    
    g
    
    h
    
    c
    
    500 4
    
    a
    
    b
    
    d
    
    h
    
    f
    
    290 3
    
    a
    
    b
    
    c
    
    a
    
    489 7
    
    b
    
    c
    
    d
    
    e
    
    f
    
    h
    
    g
    
    g
    
    0 0
    
    e
    
    1789 2
    
    f
    
    h
    
    d
    
    2000 5
    
    a
    
    b
    
    h
    
    f
    
    e
    
    b
    
    192 5
    
    a
    
    c
    
    h
    
    g
    
    d
    
    h
    
    0 2
    
    a
    
    b
    
    ------- test 8 ----
    10
    
    paul
    
    stan
    
    mark
    
    doug
    
    fred
    
    bill
    
    hank
    
    rich
    
    mike
    
    john
    
    paul
    
    0 0
    
    john
    
    300 2
    
    paul
    
    stan
    
    stan
    
    1000 1
    
    paul
    
    mark
    
    2000 3
    
    paul
    
    stan
    
    doug
    
    doug
    
    510 2
    
    paul
    
    stan
    
    fred
    
    1560 2
    
    paul
    
    stan
    
    bill
    
    178 2
    
    paul
    
    stan
    
    hank
    
    97 2
    
    paul
    
    stan
    
    rich
    
    1999 2
    
    paul
    
    stan
    
    mike
    
    1531 2
    
    paul
    
    stan
    
    ------- test 9 ----
    10
    
    paul
    
    stan
    
    mark
    
    doug
    
    fred
    
    bill
    
    hank
    
    rich
    
    mike
    
    john
    
    paul
    
    1693 6
    
    stan
    
    mark
    
    doug
    
    fred
    
    bill
    
    hank
    
    john
    
    1843 3
    
    hank
    
    mike
    
    paul
    
    stan
    
    1346 9
    
    paul
    
    mark
    
    fred
    
    bill
    
    doug
    
    hank
    
    rich
    
    mike
    
    john
    
    mark
    
    1657 1
    
    paul
    
    doug
    
    1256 9
    
    paul
    
    stan
    
    bill
    
    mark
    
    fred
    
    rich
    
    hank
    
    mike
    
    john
    
    fred
    
    1250 6
    
    paul
    
    stan
    
    bill
    
    rich
    
    john
    
    mike
    
    bill
    
    1999 2
    
    john
    
    mike
    
    hank
    
    2000 8
    
    stan
    
    mark
    
    doug
    
    fred
    
    rich
    
    bill
    
    mike
    
    john
    
    rich
    
    1999 3
    
    paul
    
    bill
    
    hank
    
    mike
    
    1999 5
    
    hank
    
    bill
    
    mark
    
    rich
    
    john
    
    

    Keep up the good work!

  • 相关阅读:
    python disable node
    Spring拥有xml配置文件和JavaConfig并存的情况
    Spring拥有多个JavaConfig(@Configuration)的情况
    阿里腾讯极其看重的数据中台,我用大白话给你解释清楚了
    f5 force offline
    JavaScript内存优化
    JavaScript内存优化
    JavaScript内存优化
    TreeMap分析(中)
    TreeMap分析(中)
  • 原文地址:https://www.cnblogs.com/lzeffort/p/3461597.html
Copyright © 2011-2022 走看看