The 11th Blue Bridge Cup-palindrome date

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 yyyymmddformat is written as an 8-bit 20200202happens to be a palindrome.

We call such a date a palindrome date.

The view was expressed 20200202that "millennium event" special day.

Xiao Ming disagrees with this, because less than 2 years later is the next palindrome date: 20211202December 2, 2021.

It was also said 20200202not just a palindrome date, or a ABABBABApalindrome date type.

Xiao Ming not agree to this, because you can experience the next about 100 years after the ABABBABApalindromic date type: 21211212that 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 ABABBABAtype of palindrome date is each day.

Note: The next palindrome date and the next ABABBABApalindrome date type may be the same day.

ABABBABAThe 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 ABABBABAtype 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:

解题思路

  1. Direct enumeration year a, will get its flip b, then a + bthat is a palindrome;
  2. By substrseparating the month and day, month and day and then determines whether legitimate;

实用函数

  1. stoi(x): Convert a string to a number;
  2. to_string(x): Convert a number to a string;
  3. substr(u, len): From ubit first, interception of a length of lenstring;
#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

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/115245351