根据输入的年月判断该月的天数 C语言
答案:3 悬赏:30 手机版
解决时间 2021-11-16 22:25
- 提问者网友:动次大次蹦擦擦
- 2021-11-16 16:30
根据输入的年月判断该月的天数 C语言
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-11-16 17:04
#include "stdio.h"
void main()
{
int y,m,d,leap=0;
printf("please input year and month:");
scanf("%d,%d",&y,&m);
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:if(y%4==0&&y%100!=0||y%400==0)d=29,leap=1;
else d=28;break;
default:printf("ERROR!");
}
if(leap)
printf("%d is a leap year and the month has %d days.",y,d);
else
printf("%d is not a leap year and the month has %d days.",y,d);
}
void main()
{
int y,m,d,leap=0;
printf("please input year and month:");
scanf("%d,%d",&y,&m);
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:d=31;break;
case 4:
case 6:
case 9:
case 11:d=30;break;
case 2:if(y%4==0&&y%100!=0||y%400==0)d=29,leap=1;
else d=28;break;
default:printf("ERROR!");
}
if(leap)
printf("%d is a leap year and the month has %d days.",y,d);
else
printf("%d is not a leap year and the month has %d days.",y,d);
}
全部回答
- 1楼网友:夜风逐马
- 2021-11-16 19:25
1、年:主要判断是否闰年,闰年2月份加1天
2、月:固定天数,写在数组中即可
2、月:固定天数,写在数组中即可
- 2楼网友:一秋
- 2021-11-16 18:28
#include
int isly(int year)
{
return ((!(year%4))&&(year%100)||(!(year%400)));
}
int dmax(int y,int m)
{
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12))
return 31;
else
{
if(m==2)
{
return 28+(isly(y));
}
else
return 30;
}
}
int main()
{
int y,m;
printf("输入年月:");
do
{
scanf("%d%d",&y,&m);
if(m<13&&m>0)
{
printf("该月有%d天.\n",dmax(y,m));
break;
}
else
printf("输入不合法,请重新输入:");
}
while(1);
return 0;
}
int isly(int year)
{
return ((!(year%4))&&(year%100)||(!(year%400)));
}
int dmax(int y,int m)
{
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12))
return 31;
else
{
if(m==2)
{
return 28+(isly(y));
}
else
return 30;
}
}
int main()
{
int y,m;
printf("输入年月:");
do
{
scanf("%d%d",&y,&m);
if(m<13&&m>0)
{
printf("该月有%d天.\n",dmax(y,m));
break;
}
else
printf("输入不合法,请重新输入:");
}
while(1);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯