Edit post
subject
Title
edit
### 題目 ![](http://topick.hket.com/res/v3/image/content/1390000/1392035/math3_1024.jpg) ### Rules 1. `A`, `C`, `E`, `G` != 0 2. 所有數字既range係[0, 9] 3. 數字冇重覆 ### Code #!/usr/bin/env pypy3 from itertools import permutations def check(i): if i[0] == 0 or i[2] == 0 or i[4] == 0 or i[6] == 0: return ab = i[0] * 10 + i[1] cd = i[2] * 10 + i[3] ef = i[4] * 10 + i[5] if ab - cd != ef: return gh = i[6] * 10 + i[7] ppp = i[8] * 100 + i[8] * 10 + i[8] if ef + gh == ppp: print("{0} - {1} = {2}, {2} + {3} = {4}".format(ab, cd, ef, gh, ppp)) if __name__ == '__main__': for i in permutations(range(0, 10), 9): check(i) ### 如果已知ppp = 111 #!/usr/bin/env pypy3 from itertools import permutations def check(i): if i[0] == 0 or i[2] == 0 or i[4] == 0 or i[6] == 0: return ab = i[0] * 10 + i[1] cd = i[2] * 10 + i[3] ef = i[4] * 10 + i[5] if ab - cd != ef: return gh = i[6] * 10 + i[7] ppp = 111 if ef + gh == ppp: print("{0} - {1} = {2}, {2} + {3} = {4}".format(ab, cd, ef, gh, ppp)) if __name__ == '__main__': for i in permutations(list(range(2, 10)) + [0], 9): check(i) 答案 85 - 46 = 39, 39 + 72 = 111 86 - 54 = 32, 32 + 79 = 111 90 - 27 = 63, 63 + 48 = 111 90 - 63 = 27, 27 + 84 = 111 95 - 27 = 68, 68 + 43 = 111 pypy3 ./primary-math.py 0.10s user 0.02s system 98% cpu 0.123 total ### 結論 出題果個正一戇鳩。 ___ ### 高效率版本 用左加強版解法既algorithm解一解原題目。唔知點解`python3`快過`pypy3`。 #!/usr/bin/env python3 from itertools import permutations PPP = 111 def check_last_4(i): if i[0] == 0 or i[2] == 0: return 0 EF, GH = i[0] * 10 + i[1], i[2] * 10 + i[3] if EF + GH == PPP: return EF return 0 def check_first_4(i, EF): if i[0] == 0 or i[2] == 0: return 0 AB, CD = i[0] * 10 + i[1], i[2] * 10 + i[3] if AB - CD == EF: return 1 if __name__ == '__main__': for i in permutations(set(range(10)) - {1}, 4): EF = check_last_4(i) if EF == 0: continue for j in permutations(set(range(10)) - set(i) - {1}, 4): if check_first_4(j, EF): print(j, i) ### 結果 (9, 0, 6, 3) (2, 7, 8, 4) (8, 6, 5, 4) (3, 2, 7, 9) (8, 5, 4, 6) (3, 9, 7, 2) (9, 0, 2, 7) (6, 3, 4, 8) (9, 5, 2, 7) (6, 8, 4, 3) python ./eff.py 0.05s user 0.00s system 98% cpu 0.055 total
Content
vpn_key
Password
Preview
Powered by
Simple Blog