描述
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
输入描述:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
输出描述:
每组数据输出一行,即日期差值
示例1
输入:
20110412
20110422
输出:
11
#include <iostream>
using namespace std;
const int monday[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){
if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0){
return true;
}return false;
}
int Number(int year,int mon,int day){
int number = 0;
for(int i = 1;i < year;i++){
if(isLeapYear(year))
number += 366;
else
number += 365;
}
for(int i = 0;i < mon;i++){
number += monday[isLeapYear(year)][i];
}
number += day;
return number;
}
int main() {
int begyear,begmon,begday;
while(~scanf("%04d%02d%02d",&begyear,&begmon,&begday)) {
int number1 = Number(begyear,begmon,begday);
int endyear, endmon, endday;
scanf("%04d%02d%02d", &endyear, &endmon, &endday);
int number2 = Number(endyear,endmon,endday);
printf("%d\n", abs(number1 - number2) + 1);
}
}