Advertisement
Spocoman

04. Personal Titles

Sep 4th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     double age;
  7.     cin >> age;
  8.  
  9.     string gender;
  10.     cin >> gender;
  11.  
  12.     if (age < 16) {
  13.         if (gender == "m") {
  14.             cout << "Master" << endl;
  15.         }
  16.         else if (gender == "f") {
  17.             cout << "Miss" << endl;
  18.         }
  19.     }
  20.     else {
  21.         if (gender == "m") {
  22.             cout << "Mr." << endl;
  23.         }
  24.         else if (gender == "f") {
  25.             cout << "Ms." << endl;
  26.         }
  27.     }
  28.  
  29.     return 0;
  30. }
  31.  
  32. Решение с тернарен оператор:
  33.  
  34. #include <iostream>
  35.  
  36. using namespace std;
  37.  
  38. int main() {
  39.     double age;
  40.     cin >> age;
  41.  
  42.     string gender;
  43.     cin >> gender;
  44.  
  45.     cout << (age < 16 ? gender == "m" ? "Master" : "Miss" : gender == "m" ? "Mr." : "Ms.") << endl;
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement