solution.cpp 1.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#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;
}