The title describes that
during the Spring Festival in 2020, there is a special date that attracts everyone's attention: February 2, 2020.
Because if this date in the yyyymmdd
format is written as an 8-bit 20200202
happens to be a palindrome.
We call such a date a palindrome date.
The view was expressed 20200202
that "millennium event" special day.
Xiao Ming disagrees with this, because less than 2 years later is the next palindrome date: 20211202
December 2, 2021.
It was also said 20200202
not just a palindrome date, or a ABABBABA
palindrome date type.
Xiao Ming not agree to this, because you can experience the next about 100 years after the ABABBABA
palindromic date type: 21211212
that is, December 12, 2121.
It's not a "one encounter in a thousand years", but a "two encounters in a thousand years" at best.
Given an 8-digit date, your next palindrome date after the calculation date and the next ABABBABA
type of palindrome date is each day.
Note: The next palindrome date and the next ABABBABA
palindrome date type may be the same day.
ABABBABA
The type of palindrome date needs to be met A≠B
.
Input format The
input contains an eight-digit integer N, which represents the date.
Output format
of the first row represents a palindromic date,
the second line indicates a ABABBABA
type of palindromic date.
Data range
For all evaluation cases, 10000101 ≤ N ≤ 89991231, to ensure that N is an 8-digit representation of a legal date.
Input sample
20200202
Output sample
20211202
21211212
Problem solution
Date processing:
解题思路
:
- Direct enumeration year
a
, will get its flipb
, thena + b
that is a palindrome; - By
substr
separating the month and day, month and day and then determines whether legitimate;
实用函数
:
stoi(x)
: Convert a string to a number;to_string(x)
: Convert a number to a string;substr(u, len)
: Fromu
bit first, interception of a length oflen
string;
#include <iostream>
#include <algorithm>
using namespace std;
int days[] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int year)
{
return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
int get_day(int year, int month)
{
if(month == 2) return 28 + check(year);
return days[month];
}
int main()
{
int n;
cin >> n;
string ans1, ans2;
bool flag1 = false, flag2 = false;
for (int i = n / 10000; i <= 9999; i ++)
{
string a = to_string(i);
string b = a;
reverse(b.begin(), b.end());
if(a + b == to_string(n)) continue;
int month = stoi(b.substr(0, 2));
int day = stoi(b.substr(2, 2));
if(month < 1 || month > 12) continue;
if(day < 1 || day > get_day(i, month)) continue;
string s1 = a.substr(0, 2);
string s2 = a.substr(2, 2);
if(!flag1) ans1 = a + b, flag1 = true;
if(!flag2 && s1 == s2 && s1[0] != s1[1]) ans2 = a + b, flag2 = true;
if(flag1 && flag2) break;
}
cout << ans1 << endl;
cout << ans2 << endl;
return 0;
}
Lanqiao Cup C/C++ Group Provincial Competition Past Years Questions