Advertisement
Spocoman

Fitness Center

Sep 19th, 2023 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int visitors, back = 0, chest = 0, legs = 0, abs = 0, proteinBar = 0, proteinShake = 0;
  9.     cin >> visitors;
  10.     cin.ignore();
  11.  
  12.     string action;
  13.  
  14.     for (int i = 0; i < visitors; i++) {
  15.         getline(cin, action);
  16.         if (action == "Back") {
  17.             back++;
  18.         }
  19.         else if (action == "Chest") {
  20.             chest++;
  21.         }
  22.         else if (action == "Legs") {
  23.             legs++;
  24.         }
  25.         else if (action == "Abs") {
  26.             abs++;
  27.         }
  28.         else if (action == "Protein bar") {
  29.             proteinBar++;
  30.         }
  31.         else {
  32.             proteinShake++;
  33.         }
  34.     }
  35.  
  36.     cout << back << " - back\n"
  37.         << chest << " - chest\n"
  38.         << legs << " - legs\n"
  39.         << abs << " - abs\n"
  40.         << proteinShake << " - protein shake\n"
  41.         << proteinBar << " - protein bar\n"
  42.         << fixed << setprecision(2)
  43.         << 100.0 * (back + chest + legs + abs) / visitors << "% - work out\n"
  44.         << 100.0 * (proteinShake + proteinBar) / visitors << "% - protein\n";
  45.  
  46.     return 0;
  47. }
  48.  
  49. Решение с тернарен оператор:
  50.  
  51. #include <iostream>
  52. #include <iomanip>
  53. #include <string>
  54.  
  55. using namespace std;
  56.  
  57. int main() {
  58.     int visitors, back = 0, chest = 0, legs = 0, abs = 0, proteinBar = 0, proteinShake = 0;
  59.     cin >> visitors;
  60.     cin.ignore();
  61.  
  62.     string action;
  63.  
  64.     for (int i = 0; i < visitors; i++) {
  65.         getline(cin, action);
  66.  
  67.         action == "Back" ? back++ :
  68.             action == "Chest" ? chest++ :
  69.             action == "Legs" ? legs++ :
  70.             action == "Abs" ? abs++ :
  71.             action == "Protein bar" ? proteinBar++ :
  72.             proteinShake++;  
  73.     }
  74.  
  75.     cout << back << " - back\n"
  76.         << chest << " - chest\n"
  77.         << legs << " - legs\n"
  78.         << abs << " - abs\n"
  79.         << proteinShake << " - protein shake\n"
  80.         << proteinBar << " - protein bar\n"
  81.         << fixed << setprecision(2)
  82.         << 100.0 * (back + chest + legs + abs) / visitors << "% - work out\n"
  83.         << 100.0 * (proteinShake + proteinBar) / visitors << "% - protein\n";
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement