输入一个年份和一个月份输出这个月有多少天。C语言
时间: 2023-10-26 15:05:25 浏览: 159
以下是一个实现的示例代码:
```
#include <stdio.h>
int main() {
int year, month;
printf("请输入年份和月份,用空格分隔:");
scanf("%d %d", &year, &month);
int days;
if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29; // 闰年2月有29天
} else {
days = 28; // 平年2月有28天
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30; // 4、6、9、11月有30天
} else {
days = 31; // 其他月份有31天
}
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
```
代码思路:
1. 从控制台输入年份和月份。
2. 根据月份判断这个月有多少天。其中,2月份需要特殊判断,闰年2月有29天,平年2月有28天。4、6、9、11月有30天,其他月份有31天。
3. 输出结果。
注意:这里假设输入的年份和月份是合法的。如果需要加入输入合法性判断,可以在输入后添加适当的检查。