使用函数和if语句写出以下函数:输入年份y和月份x,输出y年x月有几天
#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("输入错误!");
return 0;
}
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:示例 1:
输入: 1926 8
输出: 31
示例 2:
输入: 2000 2
输出: 29
#include <stdio.h>
int main(void)
{
int year,month;
scanf("%d %d",&year,&month);
if( ((year%100 !=0 && year%4 == 0) || year%400 == 0))
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
printf("31");
}
else if(month==2)
{
printf("29");
}
else
{
printf("30");
}
}
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
printf("31");
}
else if(month==2)
{
printf("28");
}
else
{
printf("30");
}
}
return 0;
}