时间限制: 1.0s 内存限制: 256.0MB 本题总分:20 分
【问题描述】
2020 年春节期间,有一个特殊的日期引起了大家的注意:2020 年 2 月 2
日。因为如果将这个日期按 “yyyymmdd” 的格式写成一个 8 位数是 20200202,
恰好是一个回文数。我们称这样的日期是回文日期。
有人表示 20200202 是 “千年一遇” 的特殊日子。对此小明很不认同,因为
不到 2 年之后就是下一个回文日期:20211202 即 2021 年 12 月 2 日。
也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA
型的回文日期。对此小明也不认同,因为大约 100 年后就能遇到下一个
ABABBABA 型的回文日期:21211212 即 2121 年 12 月 12 日。算不上 “千
年一遇”,顶多算 “千年两遇”。
给定一个 8 位数的日期,请你计算该日期之后下一个回文日期和下一个
ABABBABA 型的回文日期各是哪一天。
【输入格式】
输入包含一个八位整数 N,表示日期。
【输出格式】
输出两行,每行 1 个八位数。第一行表示下一个回文日期,第二行表示下
一个 ABABBABA 型的回文日期。
【样例输入】
20200202
【样例输出】
20211202
21211212
试题 G: 回文日期 9
第十一届蓝桥杯大赛软件类省赛第二场 C/C++ 大学 B 组
【评测用例规模与约定】
对于所有评测用例,10000101 ≤ N ≤ 89991231,保证 N 是一个合法日期的
8 位数表示。
反思:哎,考试的时候忘了关闭同步与进行优化,时间复杂度高了点,可能一些样例过不去。
因为数是固定的8位数,所以手动获取数进行integer与string类型的转换要比流式输入输出快
判断回文串也是。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<sstream>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 100100;
string i2s(int y,int m,int d){
//2020 2 3 20200203
//2020 3 31 20200331
//2020 11 3 20201103
//2020 11 30 20201130
string str="";
str = str + (char)(y/1000 + '0') + (char)(y/100%10 + '0') + (char)(y/10%10 + '0') + (char)(y%10 + '0');
str = str + (char)(m/10 + '0') + (char)(m%10 + '0') ;
str = str + (char)(d/10 + '0') + (char)(d%10 + '0') ;
return str;
}
bool judge_1(string str){
int len = str.length();
int n = len/2;
for(int i=0;i<n;i++)
if(str[i]!=str[len-i-1]) return false;
return true;
}
bool judge_2(string str){
// 01234567
// ABABBABA
// 21211212
int len=str.length();
set<char> st;
for(int i=0;i<len;i++)
st.insert(str[i]);
//
if(st.size()!=2) return false;
if( (str[0]==str[2]&&str[2]==str[5]&&str[5]==str[7]) &&
(str[1]==str[3]&&str[3]==str[4]&&str[4]==str[6]) )
return true;
return false;
}
bool isLeap(int y){
// 28 29
if( y%400==0 || ( y%100!=0 && y%4==0) )
return true;
return false;
}
int main(){
// ios::sync_with_stdio(false);
int n;
int y,m,d;
cin>>n;
y = n / 10000 ;
m = n/100%100 ;
d = n%100;
//cout<<y<<" "<<m<<" "<<d;
int f1=0,f2=0;
while(true){
d++;
// day
if( d==32 && (m==1||m==3||m==5||m==7||m==8||m==10||m==12) ){//31
d=1;
m++;
}
else if( d==31 && (m==4||m==6||m==9||m==11 ) ){//30
d=1;
m++;
}
else if(m==2){
if(d==30 && isLeap(y)==true){//29
d=1;
m++;
}else if(d==29 && isLeap(y)==false){//28
d=1;
m++;
}
}
if(m==13){
m=1;
y++;
}
///
//cout<<y<<" "<<m<<" "<<d<<endl;
//2020 2 3
//2020 3 31
//2020 11 3
//2020 11 30
//
string str = i2s(y,m,d);
//cout<<str<<endl;
if(judge_1(str)){
if(f1==0){
cout<<str<<endl;
f1=1;
}
if(judge_2(str)){
cout<<str<<endl;
f2=1;
}
}
if(f1&&f2) break;
}
return 0;
}