Advertisement
Spocoman

01. Day of Week

Sep 4th, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int number;
  7.     cin >> number;
  8.  
  9.     switch (number) {
  10.     case 1:
  11.         cout << "Monday" << endl;
  12.         break;
  13.     case 2:
  14.         cout << "Tuesday" << endl;
  15.         break;
  16.     case 3:
  17.         cout << "Wednesday" << endl;
  18.         break;
  19.     case 4:
  20.         cout << "Thursday" << endl;
  21.         break;
  22.     case 5:
  23.         cout << "Friday" << endl;
  24.         break;
  25.     case 6:
  26.         cout << "Saturday" << endl;
  27.         break;
  28.     case 7:
  29.         cout << "Sunday" << endl;
  30.         break;
  31.     default:
  32.         cout << "Error" << endl;
  33.         break;
  34.     }
  35.  
  36.     return 0;
  37. }
  38.  
  39. Решение с тернарен оператор:
  40.  
  41. #include <iostream>
  42.  
  43. using namespace std;
  44.  
  45. int main() {
  46.     int number;
  47.     cin >> number;
  48.  
  49.     string dayOfWeek =
  50.         number == 1 ? "Monday" :
  51.         number == 2 ? "Tuesday" :
  52.         number == 3 ? "Wednesday" :
  53.         number == 4 ? "Thursday" :
  54.         number == 5 ? "Friday" :
  55.         number == 6 ? "Saturday" :
  56.         number == 7 ? "Sunday" : "Error";
  57.  
  58.     cout << dayOfWeek << endl;
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement