Advertisement
Spocoman

08. Tennis Ranklist

Sep 7th, 2023
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int numberOfTournaments, startPoints;
  7.     cin >> numberOfTournaments >> startPoints;
  8.  
  9.     int tournamentPoints = 0;
  10.     int gamesWon = 0;
  11.  
  12.     string currentTournament;
  13.  
  14.     for (int i = 0; i < numberOfTournaments; i++) {
  15.         cin >> currentTournament;
  16.  
  17.         if (currentTournament == "W") {
  18.             tournamentPoints += 2000;
  19.             gamesWon++;
  20.         }
  21.         else if (currentTournament == "F") {
  22.             tournamentPoints += 1200;
  23.         }
  24.         else {
  25.             tournamentPoints += 720;
  26.         }
  27.     }
  28.  
  29.     cout.setf(ios::fixed);
  30.     cout.precision(2);
  31.  
  32.     cout << "Final points: " << startPoints + tournamentPoints << endl;
  33.     cout << "Average points: " << tournamentPoints / numberOfTournaments << endl;
  34.     cout << 100.0 * gamesWon / numberOfTournaments << '%' << endl;
  35.  
  36.     return 0;
  37. }
  38.  
  39. Решение с тернарен оператор:
  40.  
  41. #include <iostream>
  42.  
  43. using namespace std;
  44.  
  45. int main() {
  46.     int numberOfTournaments, startPoints;
  47.     cin >> numberOfTournaments >> startPoints;
  48.  
  49.     int tournamentPoints = 0;
  50.     int gamesWon = 0;
  51.  
  52.     string currentTournament;
  53.  
  54.     for (int i = 0; i < numberOfTournaments; i++) {
  55.         cin >> currentTournament;
  56.        
  57.         tournamentPoints +=
  58.             currentTournament == "W" ? 2000 :
  59.             currentTournament == "F" ? 1200 : 720;
  60.  
  61.         if (currentTournament == "W") {
  62.             gamesWon++;
  63.         }
  64.     }
  65.  
  66.     cout.setf(ios::fixed);
  67.     cout.precision(2);
  68.  
  69.     cout << "Final points: " << startPoints + tournamentPoints << endl;
  70.     cout << "Average points: " << tournamentPoints / numberOfTournaments << endl;
  71.     cout << 100.0 * gamesWon / numberOfTournaments << '%' << endl;
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement