只需要枚举年份就可以
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
typedef long long LL;
void i2s(int num, string &s) { //数字转字符串
stringstream ss;
ss << num;
ss >> s;
}
void s2i(int &num, string s) { //字符串转数字
stringstream ss;
ss << s;
ss >> num;
}
bool isLeap(int year) {
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
bool judgeDay(int year, int month, int day) { //闰年非闰年分开判断
int LeapMonthList[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int NoLeapMonthList[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( (isLeap(year) && day >= 1 && day <= LeapMonthList[month]) ||
(!isLeap(year) && day >= 1 && day <= NoLeapMonthList[month]) ) {
return true;
}
return false;
}
int main() {
LL n;
cin >> n;
n /= 10000; //取得年份
bool flag = false;
for (int i = n + 1; i <= 8999; i++) {
string num;
i2s(i, num);
//string temp = num;
reverse(num.begin(), num.end());
string pre = num.substr(0, 2), nex = num.substr(2, 4);
int month, day;
s2i(month, pre);
s2i(day, nex);
if (month >= 1 && month <= 12) { //是0-12的月份
if (judgeDay(i, month, day) && !flag) { //判断天数
cout << i << pre << nex << endl;
flag = true;
}
//判断ABABBABA
if (judgeDay(i, month, day) && num[0] == num[2] && num[1] == num[3] && num[0] != num[1]) {
cout << i << pre << nex << endl;
break;
}
} else {
continue;
}
}
return 0;
}