Advertisement
Spocoman

02. Bike Race

Sep 5th, 2023
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int juniors, seniors;
  7.     cin >> juniors >> seniors;
  8.  
  9.     string trace;
  10.     cin >> trace;
  11.  
  12.     double juniorPrice;
  13.     double seniorPrice;
  14.  
  15.     if (trace == "trail") {
  16.         juniorPrice = 5.5;
  17.         seniorPrice = 7;
  18.     }
  19.     else if (trace == "cross-country") {
  20.         juniorPrice = 8;
  21.         seniorPrice = 9.5;
  22.     }
  23.     else if (trace == "downhill") {
  24.         juniorPrice = 12.25;
  25.         seniorPrice = 13.75;
  26.     }
  27.     else if (trace == "road") {
  28.         juniorPrice = 20;
  29.         seniorPrice = 21.5;
  30.     }
  31.  
  32.     double totalSum = juniors * juniorPrice + seniors * seniorPrice;
  33.  
  34.     if (juniors + seniors >= 50 && trace == "cross-country") {
  35.         totalSum *= 0.75;
  36.     }
  37.  
  38.     totalSum *= 0.95;
  39.  
  40.     printf("%.2f\n", totalSum);
  41.  
  42.     return 0;
  43. }
  44.  
  45. Решение с тернарен оператор:
  46.  
  47. #include <iostream>
  48.  
  49. using namespace std;
  50.  
  51. int main() {
  52.     int juniors, seniors;
  53.     cin >> juniors >> seniors;
  54.  
  55.     string trace;
  56.     cin >> trace;
  57.  
  58.     double juniorPrice =
  59.         trace == "trail" ? 5.50 :
  60.         trace == "cross-country" ? 8.00 :
  61.         trace == "downhill" ? 12.25 : 20;
  62.    
  63.     double seniorPrice =
  64.         trace == "trail" ? 7.00 :
  65.         trace == "cross-country" ? 9.50 :
  66.         trace == "downhill" ? 13.75 : 21.50;
  67.  
  68.     double totalSum = (juniors * juniorPrice + seniors * seniorPrice)
  69.         * (juniors + seniors >= 50 && trace == "cross-country" ? 0.75 : 1) * 0.95;
  70.  
  71.     printf("%.2f\n", totalSum);
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement