Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board Gift Giving Download Source Code
Author Details Code Information
Cinjection ( Oleksi Derkatch )

Pinned 13 Codes
Posted 8 Coding Articles

Send A Message
View Coders Profile
Language C++
Expires Never
Length 1,169 Characters (80 Lines)
Password no password
Description

For Gleb. Solves one of the USACO contest questisons.
  1. /*
  2. ID: cinject1
  3. PROG: gift1
  4. TASK: gift1
  5. LANG: C++
  6. */
  7. #include <iostream>
  8. #include <fstream>
  9. #include <map>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16.           ifstream in("gift1.in");
  17.           ofstream out("gift1.out");
  18.  
  19.           map<string, int> friends;
  20.  
  21.           int nFriends(0);
  22.  
  23.           in>>nFriends;
  24.  
  25.           string *indexes = new string[nFriends];
  26.  
  27.           //read in friend names
  28.           for (int l=0; l < nFriends; l++)
  29.           {
  30.                     string name;
  31.                     in>>name;
  32.                     friends[name] = 0;
  33.                     indexes[l] = name;
  34.  
  35.           }
  36.  
  37.           //Begin the madness!
  38.           for (int i=0; i < nFriends; i++)
  39.           {
  40.                     string from;
  41.  
  42.                     int amount(0);
  43.                     int between(0);
  44.                     int toGive(0);
  45.  
  46.                     in>>from;
  47.                     in>>amount;
  48.                     in>>between;
  49.  
  50.                     friends[from] -= amount;
  51.                     if (between)
  52.                     {
  53.                               toGive = amount / between;
  54.                               friends[from] += amount % between;
  55.                     }
  56.                     else
  57.                     {
  58.                               friends[from] -= amount;
  59.                     }
  60.  
  61.                     for (int j=0; j < between; j++)
  62.                     {
  63.                               string who;
  64.                               in>>who;
  65.  
  66.                               friends[who] += toGive;
  67.                     }
  68.           }
  69.  
  70.           //Output the state after the aforementioned madness
  71.           for (int iter=0; iter < nFriends; iter++)
  72.           {
  73.                     out<< indexes[iter] <<" "<< friends[ indexes[iter] ] <<endl;
  74.           }
  75.  
  76.           delete [] indexes;
  77.  
  78.           return 0;
  79.  
  80. }
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
Page 1 of 1
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved