c语言读入一个年份和月份,打印出该月有多少天(考虑闰年)
#include<stdio.h>#include<math.h>intmain(){intmonth,year;charch;scanf("%d%c%d",&year,...
#include<stdio.h>
#include<math.h>
int main()
{
int month,year;
char ch;
scanf("%d%c%d",&year,&ch,&month);
if(fabs(float(year)/100-year/100==0)
if(fabs(float(year)/400-year/400)==0)
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("29");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("28");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else if(fabs(float(year)/4-year/4)==0)
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("29");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("28");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
return 0;
}
错了不少,自己找不到,求大神帮改 展开
#include<math.h>
int main()
{
int month,year;
char ch;
scanf("%d%c%d",&year,&ch,&month);
if(fabs(float(year)/100-year/100==0)
if(fabs(float(year)/400-year/400)==0)
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("29");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("28");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else if(fabs(float(year)/4-year/4)==0)
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("29");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
else
swith(month)
{
case 1&&3&&5&&7&&8&&10&&12:
printf("31");
break;
case 2:
printf("28");
break;
case 4&&6&&9&&11:
printf("30");
break;
}
return 0;
}
错了不少,自己找不到,求大神帮改 展开
2个回答
展开全部
呃。。。太长了
试试我的吧:
输入样例:年份 月份(2012 2)
#include<stdio.h>
int month[2][12]={31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31};/*把月份的天数储存下来*/
int run(int year){//判断闰年的函数,1代表闰年
if(year%4==0 && !(year%100==0 && year%400!=0)) return 1;
else return 0;
}
int main(){
while(1){
int year,mon;
scanf("%d%d",&year,&mon);
if(year==0) break;//输入年份为0,程序结束
printf("当月天数为: %d\n",month[run(year)][mon-1]);
}
return 0;
}
展开全部
程序太乱了,思路和方法及语法错了一大堆!
scanf("%d/%d", &year,&month); //输入样例2014/9
switch( month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31\n");
break;
case 4:
case 6:
case 9:
case 11:
printf("30\n");
break;
case 2:
if ( (year %4==0 && year %100 !=0 ) || year %400==0 )
{
printf("29\n");
break;
}
else
{
printf("28\n");
break;
}
default:
printf("error month\n");
break;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询