这道题就是个细节模拟,细节还是有点多的,要小心(主要是几个情况要想全)
题目
Example
Input
3
2111 02 01 2111 02 03
2202 01 01 2202 12 31
2000 01 01 9999 12 31
Output
1
365
44294
Note
In the first sample case, 21110202 is the only Day for Disaster Reduction.
In the second sample case, 202 is a substring of 2202, so every day in the year 2202 is the Day for Disaster Reduction!
解释和思路
这道题就是给你两个日期,然后找中间出现过子序列202的天数统计输出
因此我们考虑两种情况:我们把数据分成完整年和残缺年,完整年就是year1<year2是中间的年,起始和终结年就是残缺年。完整年如果有202在年位出现,那么显然一整年都算是出现过202,然后根据是否是闰年判断加365或366;如果是残缺年的情况,那么就跑暴力遍历,验证每一天。当然如果都不是,每一年可以加2天,那就是12月2号和2月2号(12.02&&02.02)
AC代码
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
typedef long long ll;
int year1,mon1,day1,year2,mon2,day2;
int month1[20] = {31,29,31,30,31,30,31,31,30,31,30,31},month2[20]={31,28,31,30,31,30,31,31,30,31,30,31};
//判断闰年
bool judge(int year)
{
if(year%400==0||(year%4==0&&year%100!=0))return 1;
else return 0;
}
//判断一月有几天,加入闰年判断
int mon(int year,int mon){
if(judge(year))mon = month1[mon-1];
else mon = month2[mon-1];
return mon;
}
//返回整年的情况
int check_year(int year)
{
if(year/10==202||year%1000==202)return 1;
if(year%10==2)return 2;
return 0;
}
//返回残缺年的情况
int check_day(int year,int mon,int day){
if((mon==12||mon==2)&&day==2)return 1;
if(year%10==2&&mon==2)return 1;
if(year%1000==202||year/10==202)return 1;
return 0;
}
int main()
{
int T,ans;
scanf("%d",&T);
while(T--)
{
ans = 0;
int flag1;
scanf("%d%d%d %d%d%d",&year1,&mon1,&day1,&year2,&mon2,&day2);
//判断整年区间的202子字符串
for(int i=year1+1;i<year2;++i)
{
flag1 = check_year(i);
if(flag1==1){
if(judge(i))ans += 366;
else ans+= 365;
}else if(flag1==2)
{
if(judge(i))ans+=30;
else ans+=29;
}else ans += 2;//只有12.02和02.02符合
}
if(year1<year2)
{
for(int i=mon1;i<=12;++i)
{
int monthNum = mon(year1, i);
for(int j=1;j<=monthNum;++j){
if(i==mon1&&j<day1)continue;//一直到起始日开始,不然都continue
if(check_day(year1, i, j))ans++;
}
}
for(int i=1;i<=mon2;++i)
{
int monthNum = mon(year2, i);
for(int j=1;j<=monthNum;++j){
if(i==mon2&&j>day2)break;;
if(check_day(year2, i, j))ans++;
}
}
}
if(year1==year2)
{
for(int i=mon1;i<=mon2;++i)
{
int monthNum = mon(year1, i);
for(int j=1;j<=monthNum;++j){
if((i==mon1&&j<day1)||(i==mon2&&j>day2))continue;
if(check_day(year1, i, j))ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}
一道细节模拟题,注意细节就好。