1 2 3 4 5 6 7 8 9 = 100

利用python的itertools求解

from itertools import product

n = 0
for s in product('+-*/ ', repeat = 8):
    e = ''
    for i in range(1, 9):
        assert(len(s) > 0)
        e += '%d' % i
        if (s[0] != ' '):
            e += s[0]
        s = s[1:]
    e += '9'

    if eval(e) == 100:
        n += 1
        print('%d#' % n, end = '\t')
        print('%s=100' % e)

运行结果

1#	1+2+3+4+5+6+7+8*9=100
2#	1+2+3-4+5+6+78+9=100
3#	1+2+3-4*5+6*7+8*9=100
4#	1+2+3-45+67+8*9=100
5#	1+2+3*4-5-6+7+89=100
6#	1+2+3*4*5/6+78+9=100
7#	1+2+3*4*56/7-8+9=100
8#	1+2+34-5+67-8+9=100
9#	1+2+34*5+6-7-8*9=100
10#	1+2-3*4+5*6+7+8*9=100
11#	1+2-3*4-5+6*7+8*9=100
12#	1+2*3+4+5+67+8+9=100
13#	1+2*3+4*5-6+7+8*9=100
14#	1+2*3-4+56/7+89=100
15#	1+2*3-4-5+6+7+89=100
16#	1+2*3*4*5/6+7+8*9=100
17#	1+2*34-56+78+9=100
18#	1+23-4+5+6+78-9=100
19#	1+23-4+56+7+8+9=100
20#	1+23-4+56/7+8*9=100
21#	1+23-4-5+6+7+8*9=100
22#	1+23*4+5-6+7-8+9=100
23#	1+23*4+56/7+8-9=100
24#	1+23*4-5+6+7+8-9=100
25#	1+234-56-7-8*9=100
26#	1+234*5*6/78+9=100
27#	1+234*5/6-7-89=100
28#	1-2+3+45+6+7*8-9=100
29#	1-2+3*4+5+67+8+9=100
30#	1-2+3*4*5+6*7+8-9=100
31#	1-2+3*4*5-6+7*8-9=100
32#	1-2-3+4*5+67+8+9=100
33#	1-2-3+4*56/7+8*9=100
34#	1-2-3+45+6*7+8+9=100
35#	1-2-3+45-6+7*8+9=100
36#	1-2-3+45-6-7+8*9=100
37#	1-2-34+56+7+8*9=100
38#	1-2*3+4*5+6+7+8*9=100
39#	1-2*3-4+5*6+7+8*9=100
40#	1-2*3-4-5+6*7+8*9=100
41#	1-23+4*5+6+7+89=100
42#	1-23-4+5*6+7+89=100
43#	1-23-4-5+6*7+89=100
44#	1*2+3+4*5+6+78-9=100
45#	1*2+3+45+67-8-9=100
46#	1*2+3-4+5*6+78-9=100
47#	1*2+3*4+5-6+78+9=100
48#	1*2+34+5+6*7+8+9=100
49#	1*2+34+5-6+7*8+9=100
50#	1*2+34+5-6-7+8*9=100
51#	1*2+34+56+7-8+9=100
52#	1*2+34-56/7+8*9=100
53#	1*2-3+4+56/7+89=100
54#	1*2-3+4-5+6+7+89=100
55#	1*2-3+4*5-6+78+9=100
56#	1*2*3+4+5+6+7+8*9=100
57#	1*2*3-4+5+6+78+9=100
58#	1*2*3-4*5+6*7+8*9=100
59#	1*2*3-45+67+8*9=100
60#	1*2*3*4+5+6+7*8+9=100
61#	1*2*3*4+5+6-7+8*9=100
62#	1*2*3*4-5-6+78+9=100
63#	1*2*34+56-7-8-9=100
64#	1*2/3+4*5/6+7+89=100
65#	1*23+4+5+67-8+9=100
66#	1*23+4+56/7*8+9=100
67#	1*23-4+5-6-7+89=100
68#	1*23-4-56/7+89=100
69#	1*23*4-56/7/8+9=100
70#	1*234+5-67-8*9=100
71#	1/2*3/4*56+7+8*9=100
72#	1/2*34-5+6-7+89=100
73#	1/2/3*456+7+8+9=100
74#	12+3+4+5-6-7+89=100
75#	12+3+4-56/7+89=100
76#	12+3-4+5+67+8+9=100
77#	12+3*4+5+6+7*8+9=100
78#	12+3*4+5+6-7+8*9=100
79#	12+3*4-5-6+78+9=100
80#	12+3*45+6*7-89=100
81#	12+34+5*6+7+8+9=100
82#	12+34-5+6*7+8+9=100
83#	12+34-5-6+7*8+9=100
84#	12+34-5-6-7+8*9=100
85#	12-3+4*5+6+7*8+9=100
86#	12-3+4*5+6-7+8*9=100
87#	12-3-4+5-6+7+89=100
88#	12-3-4+5*6+7*8+9=100
89#	12-3-4+5*6-7+8*9=100
90#	12*3-4+5-6+78-9=100
91#	12*3-4-5-6+7+8*9=100
92#	12*3-4*5+67+8+9=100
93#	12/3+4*5-6-7+89=100
94#	12/3+4*5*6-7-8-9=100
95#	12/3+4*5*6*7/8-9=100
96#	12/3/4+5*6+78-9=100
97#	123+4-5+67-89=100
98#	123+4*5-6*7+8-9=100
99#	123+45-67+8-9=100
100#	123-4-5-6-7+8-9=100
101#	123-45-67+89=100

本文地址:https://blog.csdn.net/taiq/article/details/109923623