题目传送门
一道枚举题……
暴力枚举数字位数、首位、等差数列的公差即可。注意公差的枚举范围,并且需要看看末尾合不合法。顺便提一下,我是用字符串存储枚举的数字的,所以写了一个 check
函数代替大于号。
Code
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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; ll x; ll top_num(ll x) { ll res; while (x) res = x % 10, x /= 10; return res; } ll len(ll x) { ll res = 0; while (x) res++, x /= 10; return res; } bool check(string a, string b) { if (a.size() > b.size()) return 1; if (a.size() < b.size()) return 0; for (int i = 0; i < a.size(); i++) { if (a[i] - '0' > b[i] - '0') return 1; if (a[i] - '0' < b[i] - '0') return 0; } return 1; } int main() { ios :: sync_with_stdio(0); cin >> x; for (ll i = len(x); i; i++) { for (ll j = top_num(x); j <= 9; j++) { for (ll d = -10; d <= 10; d++) { string s = to_string(j); if (j + (i - 1) * d >= 0 and j + (i - 1) * d <= 9) { for (ll k = 1; k < i; k++) s += to_string(j + k * d); } if (check(s, to_string(x))) { cout << s; return 0; } } } } return 0; }
|