題解
這一題許多小伙伴暴力枚舉,其實沒有必要,主要還是要看規律。
首先來看第一個輸出答案,其實我們只需要看回文日期的前四個數即可,不足向上加一,滿足ABCDDCBA > 輸入數據即可。
然后來看第二個輸出答案,其實我們只需要看回文日期的前兩個數即可,不足向上加一,滿足ABABBABA > 輸入數據即可。
這里的A、B不知是否能相同,我當時把這種情況否了。
#include <iostream> using namespace std; const int MAXN = 25; string s; int a[MAXN], ans, p1, p2, p3; void Print(int *a, int ans, int p1, int p2, int p3) { int tmp = p1; if (p3 <= p2) { tmp++; } cout << tmp; while (tmp) { cout << tmp % 10; tmp /= 10; } cout << endl; //拆前兩個數字 //20 int x = p1 / 100; if (x / 10 == x % 10) ++x; int sum1 = p1 * 10000 + p2; //20200202 //20200000 + 200 + 2 = 20200202 int sum2 = x * 1000000 + x * 10000 + (x % 10 * 10 + x / 10) * 100 + (x % 10 * 10 + x / 10); if (sum1 >= sum2) { ++x; if (x / 10 == x % 10) ++x; sum2 = x * 1000000 + x * 10000 + (x % 10 * 10 + x / 10) * 100 + (x % 10 * 10 + x / 10); } cout << sum2 << endl; } int main() { cin >> s; for (int i = 0; i < s.size(); ++i) { a[ans++] = s[i] - '0'; } //p1 前四位 //p2 后四位 //p3 前四位的逆序 p1 = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3]; p2 = 1000 * a[4] + 100 * a[5] + 10 * a[6] + a[7]; p3 = 1000 * a[3] + 100 * a[2] + 10 * a[1] + a[0]; Print(a, ans, p1, p2, p3); return 0; }
最后吐槽一下,當時調了很長時間沒調出來(嗯,我覺得和機房的鍵盤有關),就直接加一了,而且第二個答案的后四個數字還輸出反了,吐了呀🤮。