- 題目描述:
-
有兩個日期,求兩個日期之間的天數,如果兩個日期是連續的我們規定他們之間的天數爲兩天
- 輸入:
-
有多組數據,每組數據有兩行,分別表示兩個日期,形式爲YYYYMMDD
- 輸出:
-
每組數據輸出一行,即日期差值
- 樣例輸入:
-
20110412 20110422
- 樣例輸出:
-
11
不細心是種病,得治。。。一個晚上的時間呀。。。
#include<iostream>
#include<algorithm>
#include<cstdio>
int Abs(int x)
{
return x < 0 ? -x : x;
}
using namespace std;
#define isyear(x) ((x%100!=0 && x%4==0) || (x%400==0 ))?1:0
//bool isyear(int x)
//{
// if ((x % 100 != 0 && x % 4 == 0) || x % 400 == 0)
// return true;
// return false;
//}
int daysOfMonth[13][2] = {
{0, 0},
{31, 31},
{28, 29},
{31, 31},
{30, 30},
{31, 31},
{30, 30},
{31, 31},
{31, 31},
{30, 30},
{31, 31},
{ 30, 30},
{31, 31}
};
struct date
{
int day;
int month;
int year;
void nextday()//計算下一天的日期
{
day++;
if (day > daysOfMonth[month][isyear(year)])//若天數超過了當月最大的天數
{
day = 1;
month++;//進入下一月
if (month > 12)
{
month = 1;
year++;//進入下一年
}
}
}
};
int buf[5001][13][32];//保留預處理的天數
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
date tmp;
int cnt = 0;
tmp.day = 1;
tmp.month = 1;
tmp.year = 0;//初始化日期爲0年1月1日
while (tmp.year != 5001)
{
buf[tmp.year][tmp.month][tmp.day] = cnt;//將該日與0年1月1日的差值保存起來
tmp.nextday();
cnt++;
}
int y1, m1, d1;
int y2, m2, d2;
while (scanf("%4d%2d%2d", &y1, &m1, &d1) != EOF)
{
scanf("%4d%2d%2d", &y2, &m2, &d2);
printf("%d\n", Abs(buf[y2][m2][d2] - buf[y1][m1][d1]) + 1);
}
return 0;
}