ACM (UVa) : 119


This problems turns out as an easy 1 if u declare a structure.

// declare an array of 10-12 elements, each of which contains the structure.

// don’t forget to clr all values, b4 starting a new set of data. Otherwise, ur structure will retain the values of previous data sets, which will appear as garbage values 4 current data sets.

Struct frnd {
integer receive
integer spend
character name [20]
} array [12] = {0, 0, ”}

// first take all the names and store it in array
continue (I = 1 to number_of_persons :: I++)
input >> array [I].name

dave 200 3 laura owen vick :: means
dave spent $ 200 among laura owen vick
laura received = 200 / 3 = $ 66 [integer division, no floating values]
owen received = 200 / 3 = $ 66
vick received = 200 / 3 = $ 66
dave spent = 200 but,
200 – (66 + 66 + 66) = 2 !! what abt this $ 2 ??
this $ 2 will be added in dave’s receive account. So,
dave received = $ 2

owen 500 1 dave ::
owen spent = $ 500
dave received = 500

// special case
suppose if, owen 500 0 :: then what shud I do ?
Owen spent = $ 500
owen received = $ 500
in this case, do not wait for inputs of receiver’s names. When number_of_persons is zero then use ‘continue’. Otherwise, sum1 will get RTE.
Sum1 !! hmm … none but u.

Use strcmp () function for matching string.
Continue while (strcmp (array [I].name, giver_name) != zero)
i++

// output specification ::
output >> array [I].name, array [I].receive – array [I].spend
do not print an extra new line at the end of ur output file

#include
#include

int main ()
{
long np, i, money, person, divide, c, flag = 0;
char giver [20], receiver [20];

while (scanf (“%ld”, &np) != EOF) {

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

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); } } if (flag == 1) printf("\n"); flag = 1; for (i = 0; i < np; i++) printf("%s %ld\n", A [i].name, A [i].receive - A [i].spend); } return 0; } [/sourcecode]

Critical input:
5
dave laura owen vick amr
dave 500 0
owen 199 1 amr
amr 4 3 vick laura owen
laura 1 0
vick 2 3 dave amr owen

Critical output:
dave 0
laura 1
owen -198
vick 1
amr 196

5 thoughts on “ACM (UVa) : 119

  1. i have been trying to solve this one for quite a while now … have you any idea why it is giving WA ?

    using namespace std;
    #include
    #include

    class person {
    int give, take;
    public:
    void reset(){ give=0; take=0; }
    void gives(int x){ give=x; }
    void takes(int x){ take+=x; }
    int assets(){ return take-give; }
    } ;

    int main(){
    // freopen(“input.txt”,”r”,stdin);
    // freopen(“output.txt”,”w”,stdout);
    char name[12][11],name1[11],name2[11];
    map people;
    int i=0,j,k,l,n;
    while(cin>>n){
    memset(name,0,sizeof(name));
    people.clear();
    if(i) cout<<endl;
    for(i=0;i>name[i]; people[name[i]].reset(); }
    for(i=0;i>name1>>k>>l;
    if(l==0) continue;
    people[name1].gives(k-k%l);
    for(j=0;j>name2;
    people[name2].takes(k/l);
    }
    }
    for(i=0;i<n;i++){
    cout<<name[i]<<" "<<people[name[i]].assets()<<endl;
    }
    i=1;
    }
    return 0;
    }

  2. #include
    #include
    #include
    #include
    #include
    using namespace std;

    int main()
    {
    int n,kase=0;
    while(scanf(“%d”,&n)!=EOF)
    {
    map group;
    int amount,person;
    char name[20];
    char names[100][100];
    memset(names,0,sizeof(names));
    for(int i=0;i<n;i++)
    {
    scanf("%s",names[i]);
    group[names[i]] = 0;
    }
    for(int i=0;i<n;i++)
    {
    int k;
    scanf("%s %d %d",name,&amount,&person);
    if(person!=0)
    {
    group[name]-=(amount – amount%person);
    for(int j=0;j<person;j++)
    {
    scanf("%s",name);
    group[name] += amount/person;
    }
    }
    }
    if(kase) puts("");
    ++kase;
    for(int j=0;j<n;j++)
    printf("%s %d\n", names[j],group[names[j]]);
    }
    }

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.