给出0~9数字,让他们+-x÷自由运算,如何筛选=1的式子?
K = '''0,1,2,3,4,5,6,7,8,9'''
n = '''+-x÷'''
# 筛选=1
print:
'''
3-2=1,
5-4=1,
3-2-0=1
........
'''
import itertools as it
a = '0123456789'
b = '+-*/'
Exp,Exp1,Exp2 = [],[],[]
for exp in it.product(a,b,a):
try:
if exp[0]!=exp[-1] and eval(''.join(exp))==1:
Exp1.append(''.join(exp))
except:
pass
for exp in it.product(a,b,a,b,a):
try:
if exp[0]!=exp[2]!=exp[-1] and exp[0]!=exp[-1] and eval(''.join(exp))==1:
Exp2.append(''.join(exp))
except:
pass
for exp in Exp1 + Exp2:
Exp.append(exp+'=1')
for exp in Exp2:
Exp.append(exp+'=1')
print(len(Exp))
for exp in Exp:
print(exp)
if 计算结果==1
number = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
operator = ['+', '-', '*', '/']
result = []
for i in range(int(input("请输入随机生成的算式个数:"))):
arithmetic = random.choice(number) + random.choice(operator) + random.choice(number)
try:
rs = str(round(eval(arithmetic), 2))
except:
pass
print(arithmetic + "=" + rs)
if rs == '1' or rs == '1.0':
result.append(arithmetic + "=" + rs)
print("结果等于1的算式:")
if len(result) == 0:
print("没有")
else:
for i in result:
print(i)