8、回文日期

题目 回文日期

image-84100f82

思路分析

确定前半部分 即可确定后半部分

所以可以枚举年份 构造日期

再检查这个日期是否合法

其一 是日期 其二 在时间范围内

代码实现

#include<bits/stdc++.h>

using namespace std;

int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

bool is_leap(int y){

	return y%100 && y%4==0 || y%400==0;

}

int getdays(int y,int m){

	return days[m]+(m==2 && is_leap(y));

}

bool checkdate(int y,int m,int d){

	if(m<1 || m>12)	return false;

	if(d<1 || d>getdays(y,m)) return false;

	return true;

}

int main()

{

	int sty,stm,std,edy,edm,edd;

	scanf("%04d%02d%02d",&sty,&stm,&std);

	scanf("%04d%02d%02d",&edy,&edm,&edd);

	int startdate=sty*10000+stm*100+std;

	int enddate=edy*10000+edm*100+edd;

//	cout<<sty<<" "<<stm<<" "<<std<<endl;

//	cout<<edy<<" "<<edm<<" "<<edd<<endl;

	int cnt=0;

	for(int i=sty;i<=edy;i++){

		int left=i;

		string temp=to_string(left);

		reverse(temp.begin(),temp.end());

		int right=stoi(temp);

		int buildy=left,buildm=right/100,buildd=right%100;

		if(checkdate(buildy,buildm,buildd)){

			int curdate=buildy*10000+buildm*100+buildd;

			if(curdate>=startdate && curdate<=enddate)

				cnt++;

		}

	}

	cout<<cnt;

	return 0;

 }

同类题型

视频讲解


⬅️ 7、成绩统计 🏠 00-刷题理模型 ➡️ 9、字串分值和