import sys, os
n4 = []
ans = [0]
oplist = ["+", "-", "*", "/"]
anslist = []
wanted = 24
given = 4
def cal(a, b, signal):
if not b and signal == "/":
return False
if signal == "+":
return a + b
elif signal == "-":
return a - b
elif signal == "*":
return a * b
elif signal == "/":
return a/b
def digui(numlist):
if len(numlist) == 2:
a, b = tuple(numlist)
for op in oplist:
if cal(a, b, op) == wanted :
anslist.append(str(a) + op + str(b) + "=" + str(wanted))
ans[0] += 1
print(anslist)
anslist.pop()
else:
for i in range(len(numlist)):
nx = numlist[:]
a = nx.pop(i)
for j in range(len(nx)):
b = nx[j]
for op in oplist:
result = cal(a, b, op)
if result == 0:
continue
else:
nx[j] = result
anslist.append(str(a) + op + str(b) + "=" + str(result))
digui(nx)
anslist.pop()
nx[j] = b
return ans[0]
for i in range(1,given+1):
temp = int(input("请输入第%d个数字:" % i))
n4.append(temp)
if digui(n4) == 0:
print('no answer')
|