# 意思是,在input终端输入:
1=1
随机生成2个,左右两边 +-x÷ 相互相等的式子
# 打印 print.
3-2 = 5+3-7
2+5-6 = 3-2
>>> import itertools as it
>>> a = '0123456789'
>>> b = '+-*/'
>>> for exp in it.product(a,b,a):
try:
if exp[0]!=exp[-1] and eval(''.join(exp))==1:
print(''.join(exp))
except:
pass
0+1
1+0
1-0
2-1
3-2
4-3
5-4
6-5
7-6
8-7
9-8
>>> 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:
print(''.join(exp))
except:
pass
0+2-1
0+3-2
0+4-3
0+5-4
0+6-5
0+7-6
0+8-7
0+9-8
0-1+2
0-2+3
0-3+4
0-4+5
0-5+6
0-6+7
0-7+8
0-8+9
0*2+1
0*3+1
0*4+1
0*5+1
0*6+1
0*7+1
0*8+1
0*9+1
0/2+1
0/3+1
0/4+1
0/5+1
0/6+1
0/7+1
0/8+1
0/9+1
1+0*2
1+0*3
1+0*4
1+0*5
1+0*6
1+0*7
1+0*8
1+0*9
1+0/2
1+0/3
1+0/4
1+0/5
1+0/6
1+0/7
1+0/8
1+0/9
1+2*0
1+3*0
1+4*0
1+5*0
1+6*0
1+7*0
1+8*0
1+9*0
1-0*2
1-0*3
1-0*4
1-0*5
1-0*6
1-0*7
1-0*8
1-0*9
1-0/2
1-0/3
1-0/4
1-0/5
1-0/6
1-0/7
1-0/8
1-0/9
1-2*0
1-3*0
1-4*0
1-5*0
1-6*0
1-7*0
1-8*0
1-9*0
1*3-2
1*4-3
1*5-4
1*6-5
1*7-6
1*8-7
1*9-8
2+0-1
2+3-4
2+4-5
2+5-6
2+6-7
2+7-8
2+8-9
2-0-1
2-1+0
2-1-0
2-4+3
2-5+4
2-6+5
2-7+6
2-8+7
2-9+8
2*0+1
2*3-5
2*3/6
2*4-7
2*4/8
2*5-9
2/6*3
2/8*4
3+0-2
3+2-4
3+4-6
3+5-7
3+6-8
3+7-9
3-0-2
3-1*2
3-2+0
3-2-0
3-2*1
3-2/1
3-4+2
3-4/2
3-6+4
3-7+5
3-8+6
3-8/4
3-9+7
3*0+1
3*1-2
3*2-5
3*2/6
3/1-2
3/6*2
4+0-3
4+2-5
4+3-6
4+5-8
4+6-9
4-0-3
4-1-2
4-1*3
4-2-1
4-3+0
4-3-0
4-3*1
4-3/1
4-5+2
4-6+3
4-6/2
4-8+5
4-9+6
4-9/3
4*0+1
4*1-3
4*2-7
4*2/8
4/1-3
4/2-1
4/8*2
5+0-4
5+2-6
5+3-7
5+4-8
5-0-4
5-1-3
5-1*4
5-3-1
5-4+0
5-4-0
5-4*1
5-4/1
5-6+2
5-7+3
5-8+4
5-8/2
5*0+1
5*1-4
5*2-9
5/1-4
6+0-5
6+2-7
6+3-8
6+4-9
6-0-5
6-1-4
6-1*5
6-2-3
6-3-2
6-4-1
6-5+0
6-5-0
6-5*1
6-5/1
6-7+2
6-8+3
6-9+4
6*0+1
6*1-5
6/1-5
6/2/3
6/3-1
6/3/2
7+0-6
7+2-8
7+3-9
7-0-6
7-1-5
7-1*6
7-2-4
7-2*3
7-3*2
7-4-2
7-5-1
7-6+0
7-6-0
7-6*1
7-6/1
7-8+2
7-9+3
7*0+1
7*1-6
7/1-6
8+0-7
8+2-9
8-0-7
8-1-6
8-1*7
8-2-5
8-3-4
8-4-3
8-5-2
8-6-1
8-7+0
8-7-0
8-7*1
8-7/1
8-9+2
8*0+1
8*1-7
8/1-7
8/2-3
8/2/4
8/4-1
8/4/2
9+0-8
9-0-8
9-1-7
9-1*8
9-2-6
9-2*4
9-3-5
9-4*2
9-5-3
9-6-2
9-7-1
9-8+0
9-8-0
9-8*1
9-8/1
9*0+1
9*1-8
9/1-8
9/3-2
连起来就得到5852个式子:
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 it.product(Exp1, Exp2):
Exp.append('='.join(exp))
for exp in it.product(Exp2, Exp1):
Exp.append('='.join(exp))
print(len(Exp))
print(Exp)
我觉得你首先得给出种子,比如左边都有哪些数字,右边都有哪些数字,然后可以用程序添加符号,这种感觉比较可靠。
你可以关注一下gp算法,利用算法中的部分步骤即可获得响应的等式