做了一个 python 的简单实现,处理了使用单个数的情况,结果中会有些重复
def f(n):
if len(n) == 1:
yield n[0], str(n[0])
else:
for i in range(1, len(n)):
nx, ny = n[:i], n[i:]
for x, ex in f(nx):
for y, ey in f(ny):
yield x + y, ‘(‘ + ex + ‘+’ + ey + ‘)’
yield x – y, ‘(‘ + ex + ‘-‘ + ey + ‘)’
yield x * y, ‘(‘ + ex + ‘*’ + ey + ‘)’
for i, e in f([1, 2, 3, 4, 5, 6, 7, 8, 9]):
if i == 2010:
print(e + ‘=’ + str(i))