刚学c语言,能力有限啊,只能用switch编了,但是编译器通过,暂时也没发现什么逻辑错误,但提交就是wrong answer。。。无语啊,是不是不能这题用switch语句啊,求教大佛,代码如下。。。(有点乱)
#include<stdio.h> //给定一个日期,输出这个日期是该年的第几天int main()
{int x,y,z,h;
while(scanf("%d/%d/%d",&x,&y,&z)!=EOF)
{
if(x%400==0||(x%4==0&&x%100!=0))
switch(y)
{case 1:h=z;break;
case 2:h=31+z;break;
case 3:
case 5:
case 7:h=60+(y-3)/2*71+z;break;
case 4:
case 6:
case 8:h=60+(y-2)/2*31+((y-2)/2-1)*30+z;break; case 9:h=60+4*31+2*30+z;break;
case 10:h=60+4*31+3*30+z;break;
case 11:h=60+5*31+3*30+z;break;
case 12:h=60+5*31+4*30+z;break; }
else
switch(y)
{case 1:h=z;break;
case 2:h=31+z;break;
case 3:
case 5:
case 7:h=59+(y-3)/2*71+z;break;
case 4:
case 6:
case 8:h=59+(y-2)/2*31+((y-2)/2-1)*30+z;break; case 9:h=59+4*31+2*30+z;break;
case 10:h=59+4*31+2*30+30+z;break;
case 11:h=59+5*31+3*30+z;break;
case 12:h=59+5*31+4*30+z;break; }
printf("%d\n",h);}
return 0;}
杭电acm2005道
答案:2 悬赏:20 手机版
解决时间 2021-02-12 22:02
- 提问者网友:心如荒岛囚我终老
- 2021-02-12 11:19
最佳答案
- 五星知识达人网友:想偏头吻你
- 2021-02-12 12:35
#include <stdio.h>
bool isleapyear(int year){
return(!(year%4)&&(year%100)||!(year%400));//判断是否为闰年
}
void main()
{int leap;
int Day=0;
int year,month,day;
static int days[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}
};
printf("please input the date(year month day):");
scanf("%d%d%d",&year,&month,&day);
leap=(int)isleapyear(year);
for(int i=0;i<month-1;i++)
Day=Day+days[leap][i];
Day=Day+day;
printf("The date you've entered is %d th day!\n",Day);
}//希望这个对你有帮助
bool isleapyear(int year){
return(!(year%4)&&(year%100)||!(year%400));//判断是否为闰年
}
void main()
{int leap;
int Day=0;
int year,month,day;
static int days[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}
};
printf("please input the date(year month day):");
scanf("%d%d%d",&year,&month,&day);
leap=(int)isleapyear(year);
for(int i=0;i<month-1;i++)
Day=Day+days[leap][i];
Day=Day+day;
printf("The date you've entered is %d th day!\n",Day);
}//希望这个对你有帮助
全部回答
- 1楼网友:等灯
- 2021-02-12 14:10
修改后的ac代码:
#include
main()
{
int y,m,d,i;
while(scanf("%4d/%2d/%2d",&y,&m,&d)==3)
{
i=0;
switch(m)
{ // 使用{}来标识switch的范围
case 12:i+=30;
case 11:i+=31;
case 10:i+=30;
case 9:i+=31; // 不要09,直接9就可以了。09不是合法常量
case 8:i+=31; // 同上
case 7:i+=30; //
case 6:i+=31; //
case 5:i+=30; //
case 4:i+=31; //
case 3:{
if(y%400==0 || (y%100!=0 && y%4==0) ) // 这里少个)
i+=29;
else
i+=28;
};
case 2:i+=31;
case 1:i+=d;
}
printf("%d\n",i);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯