描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
输入描述:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出描述:
每组数据输出一行,即日期差值
示例1
输入:
20110412
20110422
输出:
11
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
bool isLeapYear(int year) {
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) return true;
else return false;
}
int date2number(int year, int month, int day) {
int number = 0;
int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
for (int i = 1; i < year; ++i) {
if (isLeapYear(i))
number += 366;
else
number += 365;
}
if (isLeapYear(year))
month_day[2] = 29;
for (int i = 1; i < month; ++i)
number += month_day[i];
number += day;
return number;
}
int main() {
int year, month, day;
while (scanf("%04d%02d%02d", &year, &month, &day) != EOF)
{
int number1 = date2number(year, month, day);
scanf("%04d%02d%02d", &year, &month, &day);
int number2 = date2number(year, month, day);
cout << abs(number2 - number1) + 1 << endl;
}
return 0;
}