社区讨论
有佬可以帮我看一下吗,有一个点一直过不了
P8716 [蓝桥杯 2020 省 AB2] 回文日期参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m32ot8uz
- 此快照首次捕获于
- 2024/11/04 15:16 去年
- 此快照最后确认于
- 2025/11/04 15:24 4 个月前
CPP
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string &date) {
return date == string(date.rbegin(), date.rend());
}
bool isABABBABA(const string &date) {
return date[0] == date[2] && date[1] == date[3] && date[0] != date[1];
}
bool isValidDate(const string &date) {
if(date.size()>8){
return false;
}
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(4, 2));
int day = stoi(date.substr(6, 2));
if (year < 1000 || year > 9999 || month < 1 || month > 12 || day < 1 || day > 31) {
return false;
}
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
if (day > 29) {
return false;
}
} else {
if (day > 28) {
return false;
}
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
return false;
}
}else{
if(day>31){
return false;
}
}
return true;
}
string nextDate(const string &date) {
int year = stoi(date.substr(0, 4));
int month = stoi(date.substr(4, 2));
int day = stoi(date.substr(6, 2));
while (true) {
if(year%400==0 || year%4==0 && year%100!=0){
if (++day > 31 || (month == 2 && day > 28) || ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)) {
day = 1;
if (++month > 12) {
month = 1;
year++;
}
}
}else{
if (++day > 31 || (month == 2 && day > 29) || ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)) {
day = 1;
if (++month > 12) {
month = 1;
year++;
}
}
}
if (month < 10 && day < 10) {
return to_string(year) + "0" + to_string(month) + "0" + to_string(day);
} else if (month < 10) {
return to_string(year) + "0" + to_string(month) + to_string(day);
} else if (day < 10) {
return to_string(year) + to_string(month) + "0" + to_string(day);
} else if (month < 13 && day < 32) {
return to_string(year) + to_string(month) + to_string(day);
}
}
}
int main() {
string date;
cin >> date;
if (!isValidDate(date)) {
return 0;
}
string nextPalinDate = nextDate(date);
while (!isPalindrome(nextPalinDate)) {
nextPalinDate = nextDate(nextPalinDate);
}
string nextABPalinDate = nextDate(nextPalinDate);
while (!(isABABBABA(nextABPalinDate) && isPalindrome(nextABPalinDate))) {
nextABPalinDate = nextDate(nextABPalinDate);
}
if(isValidDate(nextPalinDate)){
cout << nextPalinDate << endl;
}
if(isValidDate(nextABPalinDate)){
cout << nextABPalinDate << endl;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...