第十一届蓝桥杯B组 2867. 回文日期

70 阅读2分钟

2867. 回文日期 - AcWing题库

先把日期模版默上:


const int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};


int is_leap(int year)
{
    if(year%4==0&&year%100||year%400==0)  //四年一闰 百年不闰 四百年再润
    return 1;
return 0;
}


int get_monthday(int y,int m)
{
 
    if(m==2) return months[m]+is_leap(y);
    return months[m];
}

这道题的思路就是枚举日期,我们再去检查每个日期是否是回文串日期,如果是回文串日期了再检查是否是ABAB型日期。

#include<bits/stdc++.h>
using namespace std;

const int months[]={0,31,28,31,30,31,30,31,31,30,31,30,31};


int is_leap(int year)
{
    if(year%4==0&&year%100||year%400==0)  //四年一闰 百年不闰 四百年再润
    return 1;
return 0;
}


int get_monthday(int y,int m)
{
 
    if(m==2) return months[m]+is_leap(y);
    return months[m];
}


void next_day(int& y,int& m,int& d)
{
	d++;
	if(d>get_monthday(y,m))
	{
		d=1;
		m++;
		if(m>12)
		{
			m=1;
			y++;
		}
	}
}

//检查回文日期 
bool check1(char s[])
{
	for(int i=0,j=7;i<j;i++,j--)
	{
		if(s[i]!=s[j])return false;
	}
return true;
}

//检查 ABAB型日期 
bool check2(char s[])
{
	char a=s[0],b=s[1],c=s[2],d=s[3];
	
	if(a==c&&b==d&&a!=b)return true;
return false;
}
int main()
{
	int y,m,d;
	scanf("%04d%02d%02d",&y,&m,&d);
	
	//开始枚举日期
	bool found1=false,found2=false; //标记回文日期和 是否找到ABAB型日期 
	
	
	char s[10]={0};
    while(!found1 || !found2)
	{
		//向后枚举日期
		next_day(y,m,d); 
		
		
		//把日期读到字符串里
		sprintf(s,"%04d%02d%02d",y,m,d); 
		
		//如果回文日期找到了
		if(check1(s))
		{
			//找到了我们就检查一下是否输出过
			if(!found1)
			{
				puts(s);
				found1=true;
			 } 
			 
			 //如果找到了ABAB型日期 
    		if(check2(s)&&!found2)
    		{
    			if(!found2)
    			{
    				puts(s);
    				found2=true;
    			}
    		}
		} 
	
	}
	
	return 0;
}