返回题解分享
讨论 / 题解分享/ 帖子详情

回文日期(编程题) - 题解

#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define LL long long
#define endl '\n'

int months[] = {
	0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

bool is_leap(int year)
{
	return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0;
}

bool check1(int date)
{
	string s = to_string(date);
	for (int i = 0, j = 7; i < 4; i++, j--)
		if (s[i] != s[j])
			return false;
	return true;
}

bool check2(int date)
{
	string s = to_string(date);
	return (s[0] == s[2] and s[2] == s[5] and s[5] == s[7] and s[1] == s[3] and s[3] == s[4] and s[4] == s[6]);
}

void solve()
{
	int n;
	cin >> n;
	int res1 = 0, res2 = 0;
	for (int i = n + 1; ; i++)
	{
		int year = i / 10000;
		int month = i / 100 % 100;
		int day = i % 100;

		if (month > 12 or month < 1 or day > 31 or day < 1) continue;

		if (is_leap(year)) months[2] = 29;
		else months[2] = 28;

		if (day > months[month]) continue;

		if (check1(i) and !res1) res1 = i;
		if (check2(i) and !res2)
		{
			res2 = i;
			break;
		}
	}
	cout << res1 << endl << res2;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	solve();

	return 0;
}
0 回复 0 转发 0 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!