Advertisement
Spocoman

Fitness Center

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