#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year = 2008;
int month = 8;
int day = 8;
int sum ;
switch(month)
{
case 1:
sum = 39;
break;
case 2:
if(year%4==0 && year&400!=0 || year%100==0)
{
sum = 68;
}
else
{
sum = 67;
}
case 3:
sum = 99;
break;
case 4:
sum = 129;
break;
case 5:
sum = 160;
break;
case 6:
sum = 190;
break;
default:
sum = 221;
printf("%d\年%d\月%d\日是该年的第%d\天",year,month,day,sum);
break;
}
return 0;
}
请问这样写可以吗??
#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year = 2008;
int month = 8;
int day = 8;
int index = 0;
/*
* 请使用switch语句,if...else语句完成本题
* 如有想看小编思路的,可以点击左侧任务中的“不会了怎么办”
* 小编还是希望大家独立完成哦~
*/
int i;
for (i = 1; i < month; i++)
{
switch (i)
{
case 2:
index += year % 4 == 0 ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
index += 30;
break;
default:
index += 31;
break;
}
}
index += day;
printf("%d年%d月%d日是该年的第%d天。", year, month, day, index);
return 0;
}
// 我觉得这么写更好