Advertisement
Spocoman

05. Game Of Intervals

Sep 8th, 2023 (edited)
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int num;
  7.     cin >> num;
  8.  
  9.     double result = 0, num0To10 = 0, num10To20 = 0, num20To30 = 0, num30To40 = 0, num40To50 = 0, invalidNum = 0, current;
  10.  
  11.     for (int i = 0; i < num; i++) {
  12.         cin >> current;
  13.  
  14.         if (current >= 0 && current < 10) {
  15.             result += current * 0.2;
  16.             num0To10++;
  17.         }
  18.         else if (current >= 10 && current < 20) {
  19.             result += current * 0.3;
  20.             num10To20++;
  21.         }
  22.         else if (current >= 20 && current < 30) {
  23.             result += current * 0.4;
  24.             num20To30++;
  25.         }
  26.         else if (current >= 30 && current < 40) {
  27.             result += 50;
  28.             num30To40++;
  29.         }
  30.         else if (current >= 40 && current <= 50) {
  31.             result += 100;
  32.             num40To50++;
  33.         }
  34.         else {
  35.             result /= 2;
  36.             invalidNum++;
  37.         }
  38.     }
  39.  
  40.     cout.setf(ios::fixed);
  41.     cout.precision(2);
  42.  
  43.     cout << result << "\n";
  44.     cout << "From 0 to 9: " << 100.0 * num0To10 / num << "%\n";
  45.     cout << "From 10 to 19: " << 100.0 * num10To20 / num << "%\n";
  46.     cout << "From 20 to 29: " << 100 * num20To30 / num << "%\n";
  47.     cout << "From 30 to 39: " << 100 * num30To40 / num << "%\n";
  48.     cout << "From 40 to 50: " << 100 * num40To50 / num << "%\n";
  49.     cout << "Invalid numbers: " << 100 * invalidNum / num << "%\n";
  50.  
  51.     return 0;
  52. }
  53.  
  54. Решение с тернарен оператор:
  55.  
  56. #include <iostream>
  57.  
  58. using namespace std;
  59.  
  60. int main() {
  61.     int num;
  62.     cin >> num;
  63.  
  64.     double result = 0, num0To10 = 0, num10To20 = 0, num20To30 = 0, num30To40 = 0, num40To50 = 0, invalidNum = 0, current;
  65.  
  66.     for (int i = 0; i < num; i++) {
  67.         cin >> current;
  68.  
  69.         result +=
  70.             current >= 0 && current < 10 ? current * 0.2 :
  71.             current >= 10 && current < 20 ? current * 0.3 :
  72.             current >= 20 && current < 30 ? current * 0.4 :
  73.             current >= 30 && current < 40 ? 50 :
  74.             current >= 40 && current <= 50 ? 100 : -1.0 * result / 2;
  75.  
  76.         current >= 0 && current < 10 ? num0To10++ :
  77.             current >= 10 && current < 20 ? num10To20++ :
  78.             current >= 20 && current < 30 ? num20To30++ :
  79.             current >= 30 && current < 40 ? num30To40++ :
  80.             current >= 40 && current <= 50 ? num40To50++ : invalidNum++;
  81.     }
  82.  
  83.     cout.setf(ios::fixed);
  84.     cout.precision(2);
  85.  
  86.     cout << result << "\n";
  87.     cout << "From 0 to 9: " << 100.0 * num0To10 / num << "%\n";
  88.     cout << "From 10 to 19: " << 100.0 * num10To20 / num << "%\n";
  89.     cout << "From 20 to 29: " << 100 * num20To30 / num << "%\n";
  90.     cout << "From 30 to 39: " << 100 * num30To40 / num << "%\n";
  91.     cout << "From 40 to 50: " << 100 * num40To50 / num << "%\n";
  92.     cout << "Invalid numbers: " << 100 * invalidNum / num << "%\n";
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement