从键盘输入一个年份year和月份month,输出该月有多少天(考虑闰年)。
时间: 2023-05-21 11:01:21 浏览: 142
以下是可以用 Python 编写的代码:
```python
year = int(input("请输入年份:"))
month = int(input("请输入月份:"))
if month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
days = 29
else:
days = 28
elif month in [4, 6, 9, 11]:
days = 30
else:
days = 31
print(f"{year}年{month}月有{days}天")
```
如果输入的年份是闰年,2月份有29天,否则有28天。4、6、9、11月份有30天,其它月份有31天。
相关问题
C++编写从键盘输入一个年份year和月份month,输出该月有多少天(考虑闰年)。
以下是代码实现:
```c
#include <stdio.h>
int main() {
int year, month, days;
printf("请输入年份和月份:");
scanf("%d%d", &year, &month);
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:
printf("输入的月份有误!\n");
return 0;
}
printf("%d年%d月有%d天。\n", year, month, days);
return 0;
}
```
这段代码可以根据输入的年份和月份计算出该月有多少天,考虑了闰年的情况。
DEV c++编写从键盘输入一个年份year和月份month,输出该月有多少天(考虑闰年)。
以下是 DEV C 的代码:
```
#include <stdio.h>
int main() {
int year, month, days;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
printf("%d 年 %d 月有 %d 天\n", year, month, days);
return 0;
}
```
阅读全文
相关推荐
















