简答题
偏难
#13220
千年一遇对称日 在 2021 年 12 月 02 日这天(20211202),有人发了这么一张图,上书“千年一遇对称日,万事圆满在今朝”。话是挺吉利的,但好像下…
千年一遇对称日
在 2021 年 12 月 02 日这天(20211202),有人发了这么一张图,上书“千年一遇对称日,万事圆满在今朝”。话是挺吉利的,但好像下一个对称日并不需要等上一千年…… 下面任给一个人的生卒年份,请你列出来这个人一辈子经过的所有对称日。
时间限制:1000
内存限制:65536
输入
输入第一行给出一个人的名字,是由不超过30个字符的英文字母和空格组成的、以回车结束的非空字符串。第二行按 `yyyymmdd - yyyymmdd` 的格式给出这个人的生卒年(`yyyy`)、月(`mm`)、日(`dd`)。 虽然生卒日期可能是虚构的,但题目保证出生日期在去世日期之前,且每个日期都是合法的,从 `00010101` 到 `99991231`。注意这里遵循“四年一闰,百年不闰,四百年再闰”的规则定义闰年,即年份是 4 的倍数,且不是 100 的倍数的,为普通闰年;年份是整百数的,必须是 400 的倍数才是闰年。闰年跟普通年的区别就是 2 月有 29 天。
输出
首先在第一行原样输出这个人的名字。随后按照时间顺序,每行输出该人所经历的一个对称日,格式与输入格式相同。注意:生卒日期也包含在内。
样例输入
Katherine Johnson 19180826 - 20200224
样例输出
Katherine Johnson 20011002 20100102 20111102 20200202
答案与解析
正确答案:<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>
这道题暂未补解析。