AcWing 2867. 回文日期
原题链接
简单
作者:
CqAq
,
2024-04-08 21:27:55
,
所有人可见
,
阅读 2
算法1
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using ll = long long;
ll n;
string ans1, ans2;
bool tag, label;
bool isrun(int year){
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
bool Legal(int y, int m, int d){
if(m > 12 || m < 1 || d > 31 || d < 1) return false;
int da;
if(isrun(y)) da = 29;
else da = 28;
if(m == 2 && d > da) return false;
int dd[] = {4,6,9,11};
for(int i = 0; i < 4; ++ i) if(m == dd[i] && d > 30) return false;
return true;
}
bool check3(int x){
int day = x % 100, month = ((x - day) / 100) % 100, year = (x - day - month) / 10000;
if(Legal(year, month, day)) return true;
return false;
}
bool check1(string s){
for(int i = 0; i < 4; ++ i){
if(s[i] != s[7-i]) return false;
}
return true;
}
bool check2(string s){
for(int i = 0; i < 8; ++ i){
if((s[i] == s[i + 1] && i != 3) || s[i] != s[7 - i] || s[0] != s[2] || s[1] != s[3]) return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = n + 1; ; ++ i){
if(!check3(i)) continue;
string s = to_string(i);
if(check1(s) && !tag) {
ans1 = s;
tag = true;
}
if(check2(s) && !label){
ans2 = s;
label = true;
}
if(tag && label) break;
}
cout << ans1 << '\n' << ans2 << '\n';
return 0;
}