答案:<pre class="brush:cpp;toolbar:false">//参考代码1
#include <iostream>
#include <string>
// 判断是否为闰年
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
// 判断日期是否合法
bool isValidDate(int year, int month, int day) {
if (month < 1 || month > 12 || day < 1) return false;
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year) && month == 2) days[1] = 29;
return day <= days[month - 1];
}
int main() {
std::string name;
std::getline(std::cin, name);
std::string birth, death;
char dash;
std::cin >> birth >> dash >> death;
std::cout << name << std::endl;
for (int year = std::stoi(birth.substr(0, 4)); year <= std::stoi(death.substr(0, 4)); ++year) {
std::string yearStr = std::to_string(year);
std::string symmetric = yearStr + std::string(yearStr.rbegin(), yearStr.rend());
int symYear = std::stoi(symmetric.substr(0, 4));
int symMonth = std::stoi(symmetric.substr(4, 2));
int symDay = std::stoi(symmetric.substr(6, 2));
if (isValidDate(symYear, symMonth, symDay)) {
if (symmetric >= birth && symmetric <= death) {
std::cout << symmetric << std::endl;
}
}
}
return 0;
}</pre><pre class="brush:cpp;toolbar:false">//参考代码2
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
bool is_leap(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int get_max_day(int year, int month) {
if (month == 2)
return is_leap(year) ? 29 : 28;
else if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
else
return 31;
}
int main() {
string name;
getline(cin, name);
cout << name << endl;
string line;
getline(cin, line);
size_t pos = line.find(" - ");
string start_str = line.substr(0, pos);
string end_str = line.substr(pos + 3);
int start_num = stoi(start_str);
int end_num = stoi(end_str);
vector<int> valid_dates;
for (int y = 1; y <= 9999; ++y) {
char y_buf[5];
snprintf(y_buf, sizeof(y_buf), "%04d", y);
string y_str(y_buf);
string rev_str(y_str.rbegin(), y_str.rend());
string m_str = rev_str.substr(0, 2);
string d_str = rev_str.substr(2, 2);
int m = stoi(m_str);
int d = stoi(d_str);
if (m < 1 || m > 12) continue;
if (d < 1) continue;
int max_day = get_max_day(y, m);
if (d > max_day) continue;
int date_num = y * 10000 + m * 100 + d;
if (date_num >= start_num && date_num <= end_num)
valid_dates.push_back(date_num);
}
for (int date : valid_dates)
printf("%08d\n", date);
return 0;
}</pre>
这道题暂未补解析。