匿名用户
9级
2011-12-12 回答
先判断年份差,然后按每4年一个闰年计算年份的日期差,
然后再判断月份差,这需要结合当前年份是否闰年计算
追问:
具体代码如何实现呢?
追答:
void func(int year, int month, int day)
{
int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalDays = 0;
int totalYeas = year - 1949;
// calculate years firstly
totalDays = totalYears/4 * (365 + 365 + 365 + 366 );
if( totalYears%4 < 3 )
totalDays += 365 * (totalYears % 4);
else
totalDays += (365 * 3 + 366);
// Month
if( month>=10 )
{
int i=0;
for( i=10; i
totalDays += monthDays[i];
}
else
{
int i=0;
for( i=10; i>=month; i-- )
totalDays -= monthDays[i];
}
// Days
totalDays += day-1;
}
追问:
你的答案没结果显示。
代码不对啊
追答:
结果你printf一下totalDays不就好了吗?
这里有一个typo:
int totalYeas = year - 1949; -> int totalYears = year - 1949;
追问:
还是没结果啊。能运行但是没结果显示出来
追答:
你会用printf吗?
这句话之后:
totalDays += day-1;
printf("%d\n", totalDays);