Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Решение със switch:
- #include <iostream>
- using namespace std;
- int main() {
- int number;
- cin >> number;
- string result = "";
- switch (number) {
- case 1:
- result = "January";
- break;
- case 2:
- result = "February";
- break;
- case 3:
- result = "March";
- break;
- case 4:
- result = "April";
- break;
- case 5:
- result = "May";
- break;
- case 6:
- result = "June";
- break;
- case 7:
- result = "July";
- break;
- case 8:
- result = "August";
- break;
- case 9:
- result = "September";
- break;
- case 10:
- result = "October";
- break;
- case 11:
- result = "November";
- break;
- case 12:
- result = "December";
- break;
- default:
- result = "Error!";
- break;
- }
- cout << result << endl;
- return 0;
- }
- Решение с тернарен оператор:
- #include <iostream>
- using namespace std;
- int main() {
- int number;
- cin >> number;
- string result =
- number == 1 ? "January" :
- number == 2 ? "February" :
- number == 3 ? "March" :
- number == 4 ? "April" :
- number == 5 ? "May" :
- number == 6 ? "June" :
- number == 7 ? "July" :
- number == 8 ? "August" :
- number == 9 ? "September" :
- number == 10 ? "October" :
- number == 11 ? "November" :
- number == 12 ? "December" : "Error!";
- cout << result << endl;
- return 0;
- }
- Решение с масив:
- #include <iostream>
- using namespace std;
- int main() {
- int number;
- cin >> number;
- string months[13] = {
- "Error!",
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December"
- };
- cout << months[number > 0 && number < 13 ? number : 0] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement