#include <stdio.h>
int main()
{
/* 定义需要计算的日期 */
int year = 2008;
int month = 8;
int day = 8;
int sum = 0;
switch(month)
{
case 1:sum=31;break;
case 2:
if(year%400==0||year%100!=0&&year%4==0)
{
sum=28;
}
else
{
sum=29;
}
break;
case 3:sum=31;break;
case 4:sum=30;break;
case 5:sum=31;break;
case 6:sum=30;break;
case 7:sum=31;break;
case 8:sum=31;break;
case 9:sum=30;break;
case 10:sum=31;break;
case 11:sum=30;break;
case 12:sum=31;break;
}
sum+=day;
if(month>=8)
sum++;
printf("%d年%d月%d日是该年的%d天",year,month,day,sum);
return 0;
}
你后面的sum算的只是每个月份的天数,比如输入三月得到的数值是31,但这跟目标函数偏离了,