Role Classification
Simulate a server of a role-playing game for two players.
Each player has an “elo”, which is a value larger the better is the player. Everyone starts with 1200 points, and nobody ever gets a lower quantity, no matter how many games are lost. Whenever there is a match, the winner gets 10 elo points, and the loser loses 10 elo points (with the limitation above). The elo of a player is kept when he or she disconnects from the server.
We have these instructions:
LOGIN j
: The player j starts a session. Ignore it if the player is already connected.
LOGOUT j
: The player j closes the session. Ignore it if the player is not connected.
PLAY j1 j2
: Indicates that j_1 has beaten j_2, with j_1 \neq j_2. Ignore it but print an error message if any of the two players is not connected.
GET_ELO j
: Print the player j (who was connected for sure previously, although now may be disconnected) with his or her current elo.
Input: several instructions for at most 105 players. Each player’s name is different and made up of only lowercase letters.
Output: For every instruction GET_ELO
(and perhaps PLAY
) print the proper output. At the end, print an empty line, the word RANKING
, and a ranking sorted in decreasing order by elo (if there is a tie, print first the alfabetically smallest name) with all the players ever connected to the server.
- A possible appraoch is to use a
set
to store the logged players and amap
to associate each player with his/her elo
#include<algorithm>
allows to sort a vectorv
usingsort(v.begin(),v.end())
//STL - Role classification
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
int main(){
;
string cmd<string> logged;
set<string,int> elo;
map
while(cin >> cmd){
;
string player1>> player1;
cin if (cmd == "LOGIN"){
.insert(player1);
loggedif (not elo.count(player1)) elo[player1] = 1200;
}
else if (cmd == "LOGOUT") logged.erase(player1);
else if (cmd == "GET_ELO") cout << player1 << ' ' << elo[player1] << endl;
else{// case of PLAY
;
string player2>> player2;
cin if (not logged.count(player1) or not logged.count(player2)) cout << "player(s) not connected" << endl;
else{
[player1] +=10;
elo[player2] = max(elo[player2]-10,1200);
elo}
}
}
<< endl << "RANKING" << endl;
cout <pair<int,string>> v;
vectorfor (auto p : elo) v.emplace_back(-p.second,p.first);
(v.begin(),v.end());
sortfor (auto p :v) cout << p.second << ' ' << -p.first << endl;
}