From 288ea8a3e09b90b179e9b4a5ac14d0a03f24363c Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sat, 16 Apr 2022 22:22:03 +0800 Subject: [PATCH] add: 220416-CN [python brute_force] --- cpp/2204/220416-CN-brute_force.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 cpp/2204/220416-CN-brute_force.py diff --git a/cpp/2204/220416-CN-brute_force.py b/cpp/2204/220416-CN-brute_force.py new file mode 100644 index 0000000..3312346 --- /dev/null +++ b/cpp/2204/220416-CN-brute_force.py @@ -0,0 +1,34 @@ +def build_max_n(n): + ret = '' + for i in range(n): + ret += '9' + return ret + + +# Get answer of n +def get_ans(n): + half_max = int(build_max_n(n)) + half_min = 1 + int(build_max_n(n - 1)) + for i in range(half_max, half_min - 1, -1): + i_full = int(str(i) + str(i)[::-1]) + ok, ans = False, -1 + for j in range(half_max, half_min - 1, -1): + l = len(str(i_full // j)) + if l < n: + continue + if l > n: + break + if i_full % j == 0 and l == n: + ok, ans = True, j + break + if ok: + print(i_full) + print(j) + return + + +# Brute force brings miracles, table generation grants you 1st prize. +if __name__ == '__main__': + for i in range(2, 9): + get_ans(i) + print()