AcWing 2867. 回文日期
原题链接
简单
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int n;
bool is_prime(int y){
if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0) return true;
else return false;
}
int limit(int y, int m){
int leap = 0;
if(is_prime(y)){
leap = 1;
}
return m == 2 ? arr[m] + leap : arr[m];
}
//判断是不是合法的日期
bool check(int d){
int y = d / 10000;
int m = d / 100 % 100;
int d1 = d % 100;
if(m < 1 || m > 12 || d1 < 1 || d1 > limit(y,m)) return false;
else return true;
}
int main(){
cin >> n;
for(int i = n / 10000; i < 10000; i++){
int date = i;
int x = i;
while(x){
date = date * 10 + x % 10;
x /= 10;
}
if(date == n) continue;//得出的结果是同一天
if(check(date)){
cout << date << endl;
break;
}
}
for(int i = n / 10000; i < 10000; i++){
int date = i;
int x = i;
while(x){
date = date * 10 + x % 10;
x /= 10;
}
if(date == n) continue;
int AB = date / 10000 % 100, BA = date / 100 % 100;
if(AB == BA) continue;
if(check(date) && date / 1000000 == AB && date % 100 == BA){
cout << date << endl;
break;
}
}
return 0;
}