算法与case顺序的关系,真诚求助
正序case,答案错误为153
#include <stdio.h>
int main()
{
int year = 2008;
int month = 8;
int day = 8;
int date=0;
switch(month)
{
case 1:date+=8;
case 2:date+=31;
case 3:
if((year%4==0&&year%100!=0)||year%400==0)
{date+=29;}
else
{date+=28;}
case 4:date+=31;
case 5:date+=30;
case 6:date+=31;
case 7:date+=30;
case 8:date+=31;
case 9:date+=31;
case 10:date+=30;
case 11:date+=31;
case 12:date+=30;
printf("%d年%d月%d日是该年的第%d天",year,month,day,date);
break;
}
return 0;
}
倒序case,答案正确为221
#include <stdio.h>
int main()
{
int year = 2008;
int month = 8;
int day = 8;
int date = 0;
switch (month)
{
case 12: date += 30;
case 11: date += 31;
case 10: date += 30;
case 9: date += 31;
case 8: date += 31;
case 7: date += 30;
case 6: date += 31;
case 5: date += 30;
case 4: date += 31;
case 3: if(year%400 != 0 && year%4 == 0){
date += 29;
}else{
date += 28;}
case 2: date += 31;
default: date += 8,printf("2008年8月8日是该年的第%d天",date);break;
}
return 0;
}