Advertisement
Spocoman

Series

Sep 21st, 2023
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     double budget, cash;
  8.     cin >> budget;
  9.  
  10.     int volume;
  11.     cin >> volume;
  12.  
  13.     string name;
  14.  
  15.     for (int i = 0; i < volume; i++) {
  16.         cin.ignore();
  17.         getline(cin, name);
  18.         cin >> cash;
  19.  
  20.         if (name == "Thrones") {
  21.             cash /= 2;
  22.         }
  23.         else if (name == "Lucifer") {
  24.             cash *= 0.6;
  25.         }
  26.         else if (name == "Protector") {
  27.             cash *= 0.7;
  28.         }
  29.         else  if (name == "TotalDrama") {
  30.             cash *= 0.8;
  31.         }
  32.         else  if (name == "Area") {
  33.             cash *= 0.9;
  34.         }
  35.  
  36.         budget -= cash;
  37.     }
  38.  
  39.     if (budget >= 0) {
  40.         printf("You bought all the series and left with %.2f lv.\n", budget);
  41.     }
  42.     else {
  43.         printf("You need %.2f lv. more to buy the series!\n", abs(budget));
  44.     }
  45.  
  46.     return 0;
  47. }
  48.  
  49. Решение с тернарен оператор:
  50.  
  51. #include <iostream>
  52. #include <string>
  53.  
  54. using namespace std;
  55.  
  56. int main() {
  57.     double budget, cash;
  58.     cin >> budget;
  59.  
  60.     int volume;
  61.     cin >> volume;
  62.  
  63.     string name;
  64.  
  65.     for (int i = 0; i < volume; i++) {
  66.         cin.ignore();
  67.         getline(cin, name);
  68.         cin >> cash;
  69.  
  70.         cash *=
  71.             name == "Thrones" ? 0.5 :
  72.             name == "Lucifer" ? 0.6 :
  73.             name == "Protector" ? 0.7 :
  74.             name == "TotalDrama" ? 0.8 :
  75.             name == "Area" ? 0.9 : 1;
  76.  
  77.         budget -= cash;
  78.     }
  79.  
  80.     budget >= 0 ?
  81.         printf("You bought all the series and left with %.2f lv.\n", budget) :
  82.         printf("You need %.2f lv. more to buy the series!\n", abs(budget));
  83.    
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement