Advertisement
Spocoman

05. Salary

Sep 7th, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int tabs, salary;
  7.     cin >> tabs >> salary;
  8.  
  9.     string apps;
  10.  
  11.     for (int i = 0; i < tabs && salary > 0; i++) {
  12.         cin >> apps;
  13.  
  14.         if (apps == "Facebook") {
  15.             salary -= 150;
  16.         }
  17.         else if (apps == "Instagram") {
  18.             salary -= 100;
  19.         }
  20.         else if (apps == "Reddit") {
  21.             salary -= 50;
  22.         }
  23.     }
  24.  
  25.     if (salary > 0) {
  26.         cout << salary << endl;
  27.     }
  28.     else {
  29.         cout << "You have lost your salary." << endl;
  30.     }
  31.    
  32.     return 0;
  33. }
  34.  
  35. Решение с тернарен оператор:
  36.  
  37. #include <iostream>
  38. #include <string>
  39.  
  40. using namespace std;
  41.  
  42. int main() {
  43.     int tabs, salary;
  44.     cin >> tabs >> salary;
  45.  
  46.     string apps;
  47.  
  48.     for (int i = 0; i < tabs && salary > 0; i++) {
  49.         cin >> apps;
  50.  
  51.         salary -=
  52.             apps == "Facebook" ? 150 :
  53.             apps == "Instagram" ? 100 :
  54.             apps == "Reddit" ? 50 : 0;
  55.     }
  56.  
  57.     cout << (salary > 0 ? to_string(salary) : "You have lost your salary.") << endl;
  58.      
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement