题解 | #日期差值#

日期差值

http://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c

include<stdio.h>

include<stdbool.h>

include<stdlib.h>

int daytab[2][13] =
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};

bool isLeapYear(int year)
{
return (year%4 == 0&&year%100!=0)||(year%400 == 0);
}
int Number(int year, int month, int day)
{
int number = 0;
for (int i = 1; i < year; ++i)
{
if (isLeapYear(i))
{
number += 366;
}
else
{
number += 365;
}
}
int row = isLeapYear(year);
for (int j = 0; j < month; ++j)
{
number += daytab[row][j];
}
number += day;
return number;
}

int main()
{
int year, month, day;
while (scanf("%04d%02d%02d", &year, &month, &day) != EOF)
{
int number1 = Number(year, month, day);
while(scanf("%04d%02d%02d", &year, &month, &day) != EOF)
{
int number2 = Number(year, month, day);
printf("%d\n", abs(number1 - number2) + 1);
}
}
return 0;
}

全部评论

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务