-
题目描述:
-
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
-
输入:
-
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD
-
输出:
-
每组数据输出一行,即日期差值
-
样例输入:
-
20110412 20110422
-
样例输出:
-
11
-
答疑:
- 解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-7819-1-1.html
#include <iostream>
#include <cstdio>
#define isLeapYear(x) (x%4==0&&x%100!=0)||x%400==0
using namespace std;
int buf[10000][13][32];
int dayOfMonth[13][2]={
0,0,
31,31,//1
28,29,
31,31,
30,30,
31,31,
30,30,
31,31,//7
31,31,
30,30,
31,31,
30,30,
31,31
};//非闰年存在[0],闰年存在[1]
struct Date{
int year;
int month;
int day;
void nextDay(){
day++;
if (day>dayOfMonth[month][isLeapYear(year)]){
day=1;
month++;
if (month>12){
month=1;
year++;
}
}
}
};
int abs(int x){
return x>0?x:-x;
}
int main (){
//initiate
Date dateTest;
dateTest.year=1;
dateTest.month=1;
dateTest.day=1;
int count=0;
int date1,date2;
int year1,month1,day1;
int year2,month2,day2;
//preprocess
while (dateTest.year<10000){
buf[dateTest.year][dateTest.month][dateTest.day]=count;
dateTest.nextDay();
count++;
}
//body
while (cin>>date1>>date2/*scanf("%4d%2d%2d",&year1,&month1,&day1)!=EOF*/){
/*
scanf("%4d%2d%2d",&year2,&month2,&day2);
*/
year1=date1/10000;
year2=date2/10000;
month1=date1/100%100;
month2=date2/100%100;
day1=date1%100;
day2=date2%100;
cout<<(abs(buf[year1][month1][day1]-buf[year2][month2][day2])+1)<<endl;
}
return true;
}