算24 – python 代码
from itertools import permutations #permutations()应用于数组元素的全排列
from fractions import Fraction #Fraction()可进行分数的四则运算
################################################################################################
a: 随机输入4个1-10的数字 b:将a中的4个数字按不同排列方式排列 显示a中的数字
################################################################################################
a = (int(input()),int(input()),int(input()),int(input())) #随机输入一个4个1-10的数字
b = list(permutations(a)) #这4个数字的不同排列方式去冗余
print(a) #显示产生的这4个数
#################################################################################################
定义两个数的四则运算并将运算结果返回给数组result
#################################################################################################
def SDDP(a,b):
sum = a+b #定义两个数的和,由于a+b=b+a,因此和只有一种结果
difference_1 = a-b #定义两个数的差之a-b
difference_2 = b-a #定义两个数的差之b-a
product = a*b #定义两个数的积,由于a*b=b*a,因此和只有一种结果
#除法比较复杂,由于除数不能为0,因此要考虑一下三种情况:
if b !=0 and a !=0:
quotient_1 = Fraction(a,b) #Fraction()可进行分数的四则运算
quotient_2 = Fraction(b,a)
elif a==0:
quotient_1 = 0
quotient_2 = 10000000
elif b==0:
quotient_1 = 10000000
quotient_2 = 0
result = [sum, difference_1,difference_2,product,quotient_1,quotient_2]
return result
#################################################################################################
定义两个数的四则运算的表达式并将运算结果返回给数组sum
#################################################################################################
def scysfh(a,b):
c = str(a)
d = str(b)
he = "("+c+"+"+d+")"
cha1 = "("+c+"-"+d+")"
cha2 = "("+d+"-"+c+")"
cheng = "("+c+"*"+d+")"
chu1 = "("+c+"/"+d+")"
chu2= "("+d+"/"+c+")"
sum = [he, cha1, cha2, cheng, chu1, chu2]
return sum
#################################################################################################
定义4个数的四则运算表达式并将运算结果返回给数组csfh
#################################################################################################
def sessfh(a,b,c,d):
x1fh =scysfh(a,b)
x11fh =scysfh(c,d)
i=0
csfh = []
while i<len(x1fh):
x2fh=scysfh(x1fh[i],c)
j = 0
while j<len(x2fh):
x3fh = scysfh(x2fh[j],d)
x31fh = scysfh(x1fh[i], x11fh[j])
csfh.extend(x3fh)
csfh.extend(x31fh)
j +=1
i += 1
return csfh
#################################################################################################
定义4个数的四则运算并将运算结果返回给数组cs
#################################################################################################
def sess(a,b,c,d):
x1 =SDDP(a,b)
x11 = SDDP(c,d)
i=0
cs = []
while i<len(x1):
x2 =SDDP(x1[i],c)
j = 0
while j<len(x2):
x3=SDDP(x2[j],d)
x31 =SDDP(x1[i],x11[j])
cs.extend(x3)
cs.extend(x31)
j +=1
i += 1
return cs
#################################################################################################
将数组b中的4个数的组合进行四则运算并将结果和表达式分别存到d和dfh
#################################################################################################
i=0
d =[]
dfh =[]
while i < len(b):
j=0
c=sess(b[i][j],b[i][j+1],b[i][j+2],b[i][j+3])
cfh = sessfh(b[i][j], b[i][j+1], b[i][j+2], b[i][j+3])
d.extend(c)
dfh.extend(cfh)
i = i+1
#################################################################################################
判断运算结果中是否有24,并提取其下表号存入bh,根据bh找到其在dfh中的运算方式并去冗余
#################################################################################################
if 24 in d:
length = len(d)
bh = [u for u in range(length)if d[u] == 24]
print (set([dfh[t] for t in bh]))
print("Yse")
else:
print ("No")
print (len(d))