#include <iostream> #include <array> using namespace std; const int n = 15; int main() { char* month_name(int); for (int i = 0; i < n; i++) cout << "Month number" << i << " - " << month_name(i) << "\n"; } char* month_name(int k) { static char* name[] = { "none","January""February","March","April","May","June","Jule","August","September","November","December" }; return(k < 1 || k>12) ? name[0] : name[k]; } Код #include <iostream> #include <array> using namespace std; const int n = 15; int main() { char* month_name(int); for (int i = 0; i < n; i++) cout << "Month number" << i << " - " << month_name(i) << "\n"; } char* month_name(int k) { static char* name[] = { "none","January""February","March","April","May","June","Jule","August","September","November","December" }; return(k < 1 || k>12) ? name[0] : name[k]; } можете изменить код так,чтобы при вводе 1 будут выведены 3 летних месяца,цифра 2 - 3 зимных ,цифра 3 - 3 осенних,цифра 4 - 3 весенних
wrjness, конечно, могу помочь с изменением кода. Вот измененный код: #include <iostream> #include <array> using namespace std; const int n = 15; int main() { char* month_name(int, int); int input; cout << "Введите число от 1 до 4: "; cin >> input; switch (input) { case 1: for (int i = 6; i <= 8; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 2: for (int i = 1; i <= 3; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 3: for (int i = 9; i <= 11; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 4: for (int i = 3; i <= 5; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; default: cout << "Некорректный ввод!\n"; break; } return 0; } char* month_name(int k, int input) { static char* name[] = { "none","January","February","March","April","May","June","Jule","August","September","November","December" }; if (input == 1) { if (k < 6 || k > 8) return name[0]; } else if (input == 2) { if (k < 1 || k > 3) return name[0]; } else if (input == 3) { if (k < 9 || k > 11) return name[0]; } else if (input == 4) { if (k < 3 || k > 5) return name[0]; } return name[k]; } Код #include <iostream> #include <array> using namespace std; const int n = 15; int main() { char* month_name(int, int); int input; cout << "Введите число от 1 до 4: "; cin >> input; switch (input) { case 1: for (int i = 6; i <= 8; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 2: for (int i = 1; i <= 3; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 3: for (int i = 9; i <= 11; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; case 4: for (int i = 3; i <= 5; i++) cout << "Month number " << i << " - " << month_name(i, input) << "\n"; break; default: cout << "Некорректный ввод!\n"; break; } return 0; } char* month_name(int k, int input) { static char* name[] = { "none","January","February","March","April","May","June","Jule","August","September","November","December" }; if (input == 1) { if (k < 6 || k > 8) return name[0]; } else if (input == 2) { if (k < 1 || k > 3) return name[0]; } else if (input == 3) { if (k < 9 || k > 11) return name[0]; } else if (input == 4) { if (k < 3 || k > 5) return name[0]; } return name[k]; } В этом коде добавлено меню для выбора нужной группы месяцев. При вводе числа от 1 до 4 будут выведены соответствующие месяцы. Функция `month_name` изменена таким образом, чтобы выводить только нужные месяцы в зависимости от выбранной группы.
#include <iostream> #include <utility> #include <string> using namespace std; pair<string,int> month_name(int k) { pair<string,int> name[] = { pair<string,int>("none",0),pair<string,int>("June",6),pair<string,int>("Jule",7),pair<string,int>("August",8),pair<string,int>("December",12),pair<string,int>("January",1),pair<string,int>("February",2),pair<string,int>("September",9),pair<string,int>("October",10),pair<string,int>("November",11), pair<string,int>("March",3),pair<string,int>("April",4),pair<string,int>("May",5)}; return(k < 1 || k>12) ? name[0] : name[k]; } int main(){ int choice = cin.get() - '0'; for (int i = (choice-1)*3; i < choice*3; i++) cout << "Month number " << month_name(i).second << " - " << month_name(i).first << "\n"; } C #include <iostream> #include <utility> #include <string> using namespace std; pair<string,int> month_name(int k) { pair<string,int> name[] = { pair<string,int>("none",0),pair<string,int>("June",6),pair<string,int>("Jule",7),pair<string,int>("August",8),pair<string,int>("December",12),pair<string,int>("January",1),pair<string,int>("February",2),pair<string,int>("September",9),pair<string,int>("October",10),pair<string,int>("November",11), pair<string,int>("March",3),pair<string,int>("April",4),pair<string,int>("May",5)}; return(k < 1 || k>12) ? name[0] : name[k]; } int main(){ int choice = cin.get() - '0'; for (int i = (choice-1)*3; i < choice*3; i++) cout << "Month number " << month_name(i).second << " - " << month_name(i).first << "\n"; }
#include <iostream> #include <array> using namespace std; const int n = 15; void printMonths(int); int main() { for (int i = 1; i <= n; i++) { cout << "Month number " << i << " - "; printMonths(i); cout << "\n"; } } void printMonths(int k) { static const array<array<string, 3>, 5> seasons = { {{"none"}, {"June", "July", "August"}, // Summer months {"December", "January", "February"}, // Winter months {"September", "October", "November"},// Autumn months {"March", "April", "May"} // Spring months } }; if (k < 1 || k > 4) { cout << seasons[0][0]; // Invalid input, display "none" } else { for (const auto& month : seasons[k]) { cout << month << " "; } } } Python #include <iostream> #include <array> using namespace std; const int n = 15; void printMonths(int); int main() { for (int i = 1; i <= n; i++) { cout << "Month number " << i << " - "; printMonths(i); cout << "\n"; } } void printMonths(int k) { static const array<array<string, 3>, 5> seasons = { {{"none"}, {"June", "July", "August"}, // Summer months {"December", "January", "February"}, // Winter months {"September", "October", "November"},// Autumn months {"March", "April", "May"} // Spring months } }; if (k < 1 || k > 4) { cout << seasons[0][0]; // Invalid input, display "none" } else { for (const auto& month : seasons[k]) { cout << month << " "; } } }