Wednesday, October 4, 2017

UVa problem solution 119 - Greedy Gift Givers


problem link:


Discuss: In this problem you can easily use vector or structure to solve this problem.
and for presentation error you can use size_t.and there is no tricky part in this problem


try yourself before see the code




#include<bits/stdc++.h>
using namespace std;
struct givers
{
   string nam;
    int amount;
};
int main()
{
    size_t T=1;

    struct givers mem[11];
    int n,i,j,cnt=0,flag=0,money,person,each;
    string check;
    while(cin >>n)
    {
    for(i=0;i<n;i++)
    {
        cin >>mem[i].nam;
        mem[i].amount=0;
    }
    for(i=0;i<n;i++)
    {
           cin >>check>>money>>person;
           if(person)
           {
           each=money/person;
          // cout <<'y';
           for(j=0;j<n;j++)
           {
               if(check==mem[j].nam)
               {
                   mem[j].amount-=each*person;
                   check.clear();
                   break;
               }
           }
           for(j=0;j<person;j++)
           {
               cin >>check;
               for(int k=0;k<n;k++)
               {
                   if(check==mem[k].nam)
                   {
                       mem[k].amount+=each;
                       check.clear();
                       break;
                   }
               }
           }
           }
    }
     if (T++ > 1)
            cout << endl;
    for(i=0;i<n;i++)
    {
        cout <<mem[i].nam<<' '<<mem[i].amount<<endl;
    }
    //cout <<endl;
    }
    return 0;
}

No comments:

Post a Comment