写个代码,编程输入年份和月份,输出每个月的天数(其中闰年二月就输出29天,平年二月输出28天
时间: 2024-10-19 21:15:27 浏览: 63
下面是代码实现:
```cpp
#include <iostream>
using namespace std;
int main()
{
int year, month;
cout << "请输入年份和月份,格式为yyyy mm:" << endl;
cin >> year >> month;
int days;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days = 29;
else
days = 28;
break;
default:
cout << "输入的月份有误!" << endl;
return 0;
}
cout << year << "年" << month << "月有" << days << "天。" << endl;
return 0;
}
```
输入年份和月份,程序会根据月份和年份判断该月有多少天,其中闰年二月有29天,平年二月有28天。
阅读全文