int num[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; intcheck(int L, int R) { int res = 0; for (int i = L; i <= R; i++) res = res * 10 + num[i]; return res; }
intmain() { int n; cin >> n; int count = 0; while (next_permutation(num, num + 9)) { for (int i = 0; i < 7; i++) for (int j = i + 1; j < 8; j++) { int a = check(0, i); int b = check(i + 1, j); int c = check(j + 1, 8); if (a * c + b == c * n) count++; } } cout << count << endl; return0; }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from itertools import * n = int(input()) bit = len(str(n)) # n的位数 cnt = 0 for num in permutations("123456789"): a, b, c = 0, 0, 0 for al inrange(bit): # a的末尾数字 a = int("".join(num[:al+1])) bLast = (n - a) * int(num[-1]) % 10 if bLast == 0: continue bl = num.index(str(bLast)) if bl <= al or bl >= 8: continue b = int("".join(num[al+1:bl+1])) c = int("".join(num[bl+1:])) if b % c == 0and n == a + b // c: cnt += 1 print(cnt)