题解 | #日期差值#
日期差值
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;
}