Advertisement
JosepRivaille

P60296: Rol classificatori

Feb 18th, 2016
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5. using namespace std;
  6.  
  7. struct stats {
  8.   bool connected;
  9.   int elo;
  10. };
  11.  
  12. bool compare_score(pair<int, string> p1, pair<int, string> p2) {
  13.   if (p1.first > p2.first) return true;
  14.   else {
  15.     if (p1.first < p2.first) return false;
  16.     else return (p1.second < p2.second);
  17.   }
  18. }
  19.  
  20. int main() {
  21.     string action, p1, p2;
  22.     stats s;
  23.     map<string, stats> M;
  24.     map<string, stats>::iterator it;
  25.     while (cin >> action) {
  26.       cin >> p1;
  27.       it = M.find(p1);
  28.       if (action == "LOGIN") {
  29.     if (it == M.end()) {
  30.       s.connected = true;
  31.       s.elo = 1200;
  32.       M.insert(make_pair(p1, s));
  33.     }
  34.     else {
  35.       it->second.connected = true;
  36.     }
  37.       }
  38.       else if (action == "LOGOUT") {
  39.     if (it != M.end()) {
  40.       it->second.connected = false;
  41.     }
  42.       }
  43.       else if (action == "GET_ELO") {
  44.     cout << it->first << " " << it->second.elo << endl;
  45.       }
  46.       else {
  47.     cin >> p2;
  48.     map<string, stats>::iterator it2 = M.find(p2);
  49.     if ((it != M.end() && it2 != M.end()) && (it->second.connected && it2->second.connected)) {
  50.       it->second.elo += 10;
  51.       it2->second.elo -= 10;
  52.       if (it2->second.elo < 1200) it2->second.elo = 1200;
  53.     }
  54.     else cout << "jugador(s) no connectat(s)" << endl;
  55.       }
  56.     }
  57.     cout << endl << "RANKING" << endl;
  58.     int tam = M.size();
  59.     vector< pair<int,string> > v(tam);
  60.     int i = 0;
  61.     for (it = M.begin(); it != M.end(); ++it) {
  62.       v[i].first = it->second.elo;
  63.       v[i].second = it->first;
  64.       ++i;
  65.     }
  66.     sort(v.begin(), v.end(), compare_score);
  67.     for (i = 0; i < v.size(); ++i) {
  68.       cout << v[i].second << " " << v[i].first << endl;
  69.     }
  70. }
  71.  
  72. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement