Advertisement
Spocoman

09. Ski Trip

Sep 5th, 2023
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     int days;
  8.     cin >> days;
  9.  
  10.     cin.ignore();
  11.  
  12.     string room;
  13.     getline(cin, room);
  14.  
  15.     string rating;
  16.     getline(cin, rating);
  17.  
  18.     int nights = days - 1;
  19.     double roomPrice = 18.00;
  20.     double apartmentPrice = 25.00;
  21.     double presidentApartmentPrice = 35.00;
  22.  
  23.     double totalSum;
  24.  
  25.     if (room == "room for one person") {
  26.         totalSum = roomPrice * nights;
  27.     }
  28.     else if (room == "apartment") {
  29.         totalSum = apartmentPrice * nights;
  30.         if (nights < 10) {
  31.             totalSum *= 0.70;
  32.         }
  33.         else if (nights >= 10 && nights < 15) {
  34.             totalSum *= 0.65;
  35.         }
  36.         else if (nights >= 15) {
  37.             totalSum *= 0.50;
  38.         }
  39.     }
  40.     else {
  41.         totalSum = presidentApartmentPrice * nights;
  42.  
  43.         if (nights < 10) {
  44.             totalSum *= 0.90;
  45.         }
  46.         else if (nights >= 10 && nights < 15) {
  47.             totalSum *= 0.85;
  48.         }
  49.         else if (nights >= 15) {
  50.             totalSum *= 0.80;
  51.         }
  52.     }
  53.  
  54.     if (rating == "positive") {
  55.         totalSum *= 1.25;
  56.     }
  57.     else {
  58.         totalSum *= 0.90;
  59.     }
  60.  
  61.     cout.setf(ios::fixed);
  62.     cout.precision(2);
  63.  
  64.     cout << totalSum << endl;
  65.  
  66.     return 0;
  67. }
  68.  
  69. Решение с тернарен оператор и printf():
  70.  
  71. #include <iostream>
  72. #include <string>
  73.  
  74. using namespace std;
  75.  
  76. int main() {
  77.     int days;
  78.     cin >> days;
  79.     cin.ignore();
  80.  
  81.     string room, rating;
  82.     getline(cin, room);
  83.     getline(cin, rating);
  84.  
  85.     int nights = days - 1;
  86.     double roomPricePerNight = room == "room for one person" ? 18.00 : room == "apartment" ? 25.00 : 35.00;
  87.  
  88.     double totalSum = roomPricePerNight * nights;
  89.  
  90.     totalSum *=
  91.         room == "apartment" ? (nights < 10 ? 0.70 : nights >= 15 ? 0.50 : 0.65) :
  92.         room == "president apartment" ? (nights < 10 ? 0.90 : nights >= 15 ? 0.80 : 0.85) : 1;
  93.  
  94.     totalSum *= rating == "positive" ? 1.25 : 0.90;
  95.  
  96.     printf("%.2f\n", totalSum);
  97.  
  98.     return 0;
  99. }
  100.  
  101. Тарикатско решение - Don't use at work! :)
  102.  
  103. #include <iostream>
  104. #include <string>
  105.  
  106. using namespace std;
  107.  
  108. int main() {
  109.    string days, room, rating;
  110.    getline(cin, days);
  111.    getline(cin, room);
  112.    getline(cin, rating);
  113.  
  114.    int nights = stoi(days) - 1;
  115.  
  116.    printf("%.2f\n", ((room == "room for one person" ? 18.00 :
  117.        room == "apartment" ? 25.00 * (nights < 10 ? 0.70 : nights >= 15 ? 0.50 : 0.65) :
  118.        35.00 * (nights < 10 ? 0.90 : nights >= 15 ? 0.80 : 0.85))
  119.        * nights * (rating == "positive" ? 1.25 : 0.90)));
  120.  
  121.    return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement