Advertisement
RnD

Greedy Gift Givers

RnD
Feb 18th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. /*
  2. ID: rnd.gde1
  3. LANG: C++
  4. PROG: gift1
  5. */
  6.  
  7. #include <fstream>
  8. using namespace std;
  9.  
  10.     ifstream inFile("gift1.in");
  11.     ofstream outFile("gift1.out");
  12.     int n, mStarting[10];
  13.     string names[10];
  14.  
  15. struct people{
  16.     string name, giveTo[10];
  17.     int money, friends;
  18. }ppl[10];
  19.  
  20. void input();
  21. int findIndex(string name);
  22. void calculations();
  23.  
  24. int main(){
  25.  
  26.     input();
  27.     calculations();
  28.  
  29.     return 0;
  30. }
  31.  
  32. void input(){
  33.     string dummy;
  34.     inFile >> n;
  35.  
  36.     for(int i=0;i<n;i++){
  37.         inFile >> names[i];
  38.     }
  39.     for(int i=0;i<n;i++){
  40.         inFile >> ppl[i].name >> ppl[i].money >> ppl[i].friends;
  41.         mStarting[i] = ppl[i].money;
  42.         for(int m=0;m<ppl[i].friends;m++){
  43.             inFile >> ppl[i].giveTo[m];
  44.         }
  45.     }
  46.  
  47.     inFile.close();
  48. }
  49.  
  50. int findIndex(string name){
  51.     for(int i=0;i<n;i++){
  52.         if(name == ppl[i].name){
  53.             return i;
  54.             break;
  55.         }
  56.     }
  57. }
  58.  
  59. void calculations(){
  60.     int temp, finalSum[10], index;
  61.  
  62.     for(int i=0;i<n;i++){
  63.         finalSum[i]=0;
  64.     }
  65.  
  66.     for(int i=0;i<n;i++){
  67.  
  68.         if(ppl[i].friends != 0){
  69.             temp = ppl[i].money/ppl[i].friends;
  70.             ppl[i].money -= temp*ppl[i].friends;
  71.         }
  72.  
  73.         for(int m=0;m<ppl[i].friends;m++){
  74.             for(int k=0;k<n;k++){
  75.                 if(ppl[i].giveTo[m] == ppl[k].name){
  76.                    finalSum[k] += temp;
  77.                 }
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     for(int i=0;i<n;i++){
  84.         index = findIndex(names[i]);
  85.         outFile << ppl[index].name << " " << ppl[index].money+finalSum[index] - mStarting[index] << endl;
  86.     }
  87.  
  88.     outFile.close();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement