Advertisement
Guest User

01. Sorting Players

a guest
Apr 19th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. void readInput(std::vector<std::string>& input) {
  7.     std::string line;
  8.     getline(std::cin, line);
  9.     while (line != "End") {
  10.         input.push_back(line);
  11.         getline(std::cin, line);
  12.     }
  13. }
  14.  
  15. void extractScore(const std::vector<std::string>& input, std::vector<int>& scores) {
  16.     for (std::string player : input) {
  17.         int indOfNameEnd = player.find(' ');
  18.         int score = player[indOfNameEnd + 1] - player[indOfNameEnd + 3];
  19.         scores.push_back(score);
  20.         //std::cout << score << std::endl;
  21.     }
  22. }
  23.  
  24. void extractNames(const std::vector<std::string>& input, std::vector<std::string>& names) {
  25.     names = input;
  26.     for (int i = 0; i < names.size(); ++i) {
  27.         names[i] = names[i].erase(names[i].find(' '), names[i].length());
  28.     }
  29. }
  30.  
  31. void dealWithMultiAttempts(std::vector<std::string>& names, std::vector<int>& scores) {
  32.     int length = names.size()-1;
  33.     for (int i = 0; i < length; ++i) {
  34.         if (names[i] == names[i+1]) {
  35.             names.erase(names.begin()+(i+1));
  36.             scores[i] += scores[i + 1];
  37.             scores.erase(scores.begin() + (i + 1));
  38.             length -= 1;
  39.             i -= 1;
  40.         }
  41.     }
  42. }
  43.  
  44. void printResult(const std::vector<std::string>& names, const std::vector<int>& scores) {
  45.     for (int i = 0; i < scores.size(); ++i) {
  46.         std::cout << names[i] << " " << scores[i] << std::endl;
  47.     }
  48. }
  49.  
  50. int main()
  51. {
  52.     std::vector<std::string> input;
  53.     std::vector<std::string> names;
  54.     std::vector<int> scores;
  55.     readInput(input);
  56.     std::sort(input.begin(), input.end());
  57.     extractScore(input, scores);
  58.     extractNames(input, names);
  59.     dealWithMultiAttempts(names, scores);
  60.     printResult(names, scores);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement