-
题目描述:
-
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天
-
输入:
-
有多组数据,每组数据有两行,分别表示两个日期,形式为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;
}