【题目链接】:http://hihocoder.com/problemset/problem/1477
【题意】
中文题
【题解】
首先,一年一年地加,把开始的年份和结束的年份之间的年根据是否为闰年;
加上365天的秒和366天的秒;
然后把开始的那一年的剩余天数加完;
再把结束的那一年的1月1号开始一直加到结束的年的月日;
把有闰秒的年月日;
判断一下,在不在两个日期之间;
(严格在两个日期之间,不能相等);
我的程序,算的时候,右端点严格会被算到;
在的话,ans++;
【Number Of WA】
8
【完整代码】
#include <cstdio>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 5000+100;
struct abc
{
int year,month,day,hour,minute,second;
};
int n;
int a[MAXN];
abc ks,js,temp,tks,tjs;
LL rest;
int day[13];
bool rn(int x)
{
if (!(x%4))
{
if (x%100)
return true;
else
{
if (x%400)
return false;
else
return true;
}
}
else
return false;
}
void change(abc &ks)
{
if (ks.second>59)
{
ks.second=0;
ks.minute++;
if (ks.minute>59)
{
ks.minute=0;
ks.hour++;
if (ks.hour == 24)
{
ks.hour=0;
ks.day++;
if (ks.day > day[ks.month])
{
ks.day = 1;
ks.month++;
if (ks.month > 12)
{
ks.year++;
ks.month = 1;
}
}
}
}
}
}
int bijiao(abc a,abc b)
{
if (a.year != b.year)
return a.year<b.year;
if (a.month!=b.month)
return a.month<b.month;
if (a.day !=b.day)
return a.day < b.day;
if (a.hour!=b.hour)
return a.hour < b.hour;
if (a.minute!=b.minute)
return a.minute < b.minute;
if (a.second!=b.second)
return a.second<b.second;
return -1;
}
bool ok()
{
int ju1 = bijiao(tks,temp),ju2 = bijiao(temp,tjs);
if (ju1==1 && ju2==1)
return true;
return false;
}
void pd(int x)
{
temp.year = x;
if (ok()) rest++;
}
int main()
{
//freopen("D:\\rush.txt","r",stdin);
scanf("%d-%d-%d %d:%d:%d",&ks.year,&ks.month,&ks.day,&ks.hour,&ks.minute,&ks.second);
scanf("%d-%d-%d %d:%d:%d",&js.year,&js.month,&js.day,&js.hour,&js.minute,&js.second);
tks = ks,tjs = js;
rest = 0;
for (int i = ks.year+1;i <= js.year-1;i++)
if (rn(i))
rest+=31622400;
else
rest+=31536000;
day[1] = day[3] = day[5] = day[7] = day[8] = day[10] = day[12] = 31;
day[4] = day[6] = day[9] = day[11] = 30;
if (ks.year<js.year)
{
int goal = ks.year+1;
if (rn(ks.year))
day[2] = 29;
else
day[2] = 28;
while (ks.year<goal)
{
ks.second++;
rest++;
change(ks);
}
ks.year = js.year;
if (rn(ks.year))
day[2] = 29;
else
day[2] = 28;
while (ks.month < js.month || ks.day < js.day || ks.hour < js.hour || ks.minute < js.minute)
{
rest++;
ks.second++;
change(ks);
}
rest+=js.second-ks.second;
}
else
if (ks.year == js.year)
{
if (rn(ks.year))
day[2] = 29;
else
day[2] = 28;
while (ks.month < js.month || ks.day < js.day || ks.hour < js.hour || ks.minute < js.minute)
{
rest++;
ks.second++;
change(ks);
}
rest+=js.second-ks.second;
}
temp.month = 6,temp.day = 30,temp.hour = 23,temp.minute = 59,temp.second = 60;
//6/30 23:59:60
pd(1972);
for (int i = 1981;i<=1983;i++) pd(i);
pd(1985);
for (int i = 1992;i<=1994;i++) pd(i);
pd(1997);
pd(2012);
pd(2015);
temp.month = 12,temp.day = 31;
for (int i = 1972;i<=1979;i++) pd(i);
pd(1987);
for (int i = 1989;i <= 1990;i++) pd(i);
pd(1995);
pd(1998);
pd(2005);
pd(2008);
pd(2016);
cout << rest << endl;
return 0;
}