C++ 代码
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
int n;
int huiwenGet,abbaGet;
int huiwenNum,abbaNum;
void huiwen (int n){
int N;
N=n;
stringstream ss; //stringstream 任意类型吐进来,任意类型吐出去,太棒了
ss<<n;
string huiwenStr;
ss>>huiwenStr;
if(huiwenStr[0]==huiwenStr[7]&& //没有卵用的枝减
huiwenStr[1]==huiwenStr[6]&&
huiwenStr[2]==huiwenStr[5]&&
huiwenStr[3]==huiwenStr[4]){
string huiwenBack=huiwenStr.substr(4); //截取后半段
huiwenBack=string(huiwenBack.rbegin(),huiwenBack.rend()); //反转 reverse 用不起,本笨蛋不知道为什么
string huiwenHead=huiwenStr.substr(0,4); //取出前半段
if(!huiwenBack.compare(huiwenHead)){
if(huiwenGet==0){
huiwenGet=1;
huiwenNum=N;
}
}
}
}
void abba(int n){ //判断第二种情况
int N;
N=n;
stringstream ss;
ss<<n;
string abbaStr;
ss>>abbaStr;
if(abbaStr[0]==abbaStr[2]&&
abbaStr[0]==abbaStr[5]&&
abbaStr[0]==abbaStr[7]&&
abbaStr[1]==abbaStr[3]&&
abbaStr[1]==abbaStr[4]&&
abbaStr[1]==abbaStr[6]&&
abbaStr[0]!=abbaStr[1]){
abbaGet=1;
abbaNum=N;
}
}
int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //之前少写了一个月,起码耽误了半小时
bool leap_year (int year,int month,int day){ //闰年判断
if(year%400==0||(year%100==0&&year%4==0))return true;
return false;
}
bool check(int n){ //检查日期合法性
int year=n/10000;
int month=(n%10000)/100;
int day=n%100;
if(month==2&&leap_year(year,month,day)==true&&day>=29)return false;
else if(day>months[month])return false;
else if(day>31)return false;
else if(month>13||month==0)return false;
return true;
}
int main(){
cin>>n;
for(int i=n+1;i<=99999999;i++){
if(huiwenGet==1&&abbaGet==1)break; //两种类型都找到了就提前结束
if(check(i)==false)continue;
huiwen(i);
abba(i);
}
cout<<huiwenNum<<endl;
cout<<abbaNum;
return 0;
}
刚开始没有想起来判断判断日期合法性这个条件,一个人在那里疯狂枝剪还是超时。
耽误了很久,写了很长,还看了眼题解了解怎么过的时间,在下真是废得出奇。
int to string,string to int ,太过不熟练,妈的