Advertisement
Spocoman

06. Truck Driver

Sep 6th, 2023
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     const int MONTHS_OF_SEASON = 4;
  8.     const double TAX_PROFIT_IN_PERCENTAGE = 0.10;
  9.  
  10.     string season;
  11.     cin >> season;
  12.  
  13.     double kilometers;
  14.     cin >> kilometers;
  15.  
  16.     double kilometerPrice;
  17.  
  18.     if (kilometers <= 5000) {
  19.         if (season == "Spring" || season == "Autumn") {
  20.             kilometerPrice = 0.75;
  21.         }
  22.         else if (season == "Summer") {
  23.             kilometerPrice = 0.9;
  24.         }
  25.         else {
  26.             kilometerPrice = 1.05;
  27.         }
  28.     }
  29.     else if (kilometers <= 10000)
  30.     {
  31.         if (season == "Spring" || season == "Autumn") {
  32.             kilometerPrice = 0.95;
  33.         }
  34.         else if (season == "Summer") {
  35.             kilometerPrice = 1.10;
  36.         }
  37.         else {
  38.             kilometerPrice = 1.25;
  39.         }
  40.     }
  41.     else {
  42.         kilometerPrice = 1.45;
  43.     }
  44.  
  45.     double totalSum = kilometers * kilometerPrice * MONTHS_OF_SEASON * (1 - TAX_PROFIT_IN_PERCENTAGE);
  46.  
  47.     cout << fixed << setprecision(2) << totalSum << endl;
  48.  
  49.     return 0;
  50. }
  51.  
  52. Решение с тернарен оператор:
  53.  
  54. #include <iostream>
  55. #include <iomanip>
  56.  
  57. using namespace std;
  58.  
  59. int main() {
  60.     const int MONTHS_OF_SEASON = 4;
  61.     const double TAX_PROFIT_IN_PERCENTAGE = 0.10;
  62.  
  63.     string season;
  64.     cin >> season;
  65.  
  66.     double kilometers;
  67.     cin >> kilometers;
  68.  
  69.     double kilometerPrice =
  70.         kilometers <= 5000 ? (season == "Spring" || season == "Autumn" ? 0.75 : season == "Summer" ? 0.90 : 1.05) :
  71.         kilometers <= 10000 ? (season == "Spring" || season == "Autumn" ? 0.95 : season == "Summer" ? 1.10 : 1.25) : 1.45;
  72.  
  73.     double totalSum = kilometers * kilometerPrice * MONTHS_OF_SEASON * (1 - TAX_PROFIT_IN_PERCENTAGE);
  74.  
  75.     cout << fixed << setprecision(2) << totalSum << endl;
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement