凑24是经典的益智游戏,多玩可以使脑筋灵活一点,但是当遇到无解的时候,就会很伤脑筋,为此,写个程序来代为计算。
运行结果,去除了重复的一些表达式:
entry: 1
entry: 4
entry: 5
entry: 6
(4/(1-(5/6))) = 24
(6/((5/4)-1)) = 24
Press any key to exit...
entry: 3
entry: 3
entry: 8
entry: 8
(8/(3-(8/3))) = 24
Press any key to exit...
附源码:
def MergeList(L1, L2):
LSum=[]
if L1==[]:
for i in range(len(L2)):
LSum.append(L2[i])
return LSum
if L2==[]:
for i in range(len(L1)):
LSum.append(L1[i])
return LSum
#fenmu:denominator fenzi:numerator
#L:0 fenzi 1 fenmu 2 format
for i in range(len(L1)):
for j in range(len(L2)):
#+
tempa=L1[i].a * L2[j].b + L1[i].b * L2[j].a
tempb=L1[i].b * L2[j].b
tempobj=data()
tempobj.a, tempobj.b = Simplify(tempa, tempb)
if tempobj.a == 0:tempobj.b=1
tempobj.c = "(%s+%s)" % (L1[i].c, L2[j].c)
LSum.append(tempobj)
#-
tempa=abs(L1[i].a * L2[j].b - L1[i].b * L2[j].a)
tempb=L1[i].b * L2[j].b
tempobj=data()
tempobj.a, tempobj.b = Simplify(tempa, tempb)
if tempobj.a == 0:tempobj.b=1
if L1[i].a * L2[j].b > L1[i].b * L2[j].a:
tempobj.c = "(%s-%s)" % (L1[i].c, L2[j].c)
else:tempobj.c = "(%s-%s)" % (L2[j].c, L1[i].c)
LSum.append(tempobj)
#*
tempa=L1[i].a * L2[j].a
tempb=L1[i].b * L2[j].b
tempobj=data()
tempobj.a, tempobj.b = Simplify(tempa, tempb)
if tempobj.a == 0:tempobj.b=1
tempobj.c = "%s*%s" % (L1[i].c, L2[j].c)
LSum.append(tempobj)
#/
if L2[j].a != 0:
tempa=L1[i].a * L2[j].b
tempb=L1[i].b * L2[j].a
tempobj=data()
tempobj.a, tempobj.b = Simplify(tempa, tempb)
if tempobj.a == 0:tempobj.b=1
tempobj.c = "(%s/%s)" % (L1[i].c, L2[j].c)
LSum.append(tempobj)
if L1[i].a != 0:
tempa=L1[i].b * L2[j].a
tempb=L1[i].a * L2[j].b
tempobj=data()
tempobj.a, tempobj.b = Simplify(tempa, tempb)
if tempobj.a == 0:tempobj.b=1
tempobj.c = "(%s/%s)" % (L2[j].c, L1[i].c)
LSum.append(tempobj)
return LSum
def Merge3List(L1,L2,L3):
LSum=[]
LSum1=MergeList(L1, MergeList(L2,L3))
LSum2=MergeList(L2, MergeList(L1,L3))
LSum3=MergeList(L3, MergeList(L1,L2))
#calc
LSum = LSum1 + LSum2 + LSum3
return LSum
def Merge4List(L1,L2,L3,L4):
LSum=[]
LSum1=MergeList(L1, Merge3List(L2,L3,L4))
LSum2=MergeList(L2, Merge3List(L1,L3,L4))
LSum3=MergeList(L3, Merge3List(L1,L2,L4))
LSum4=MergeList(L4, Merge3List(L1,L2,L3))
LSum5=MergeList(MergeList(L1,L2), MergeList(L3,L4))
LSum6=MergeList(MergeList(L1,L3), MergeList(L2,L4))
LSum7=MergeList(MergeList(L1,L4), MergeList(L2,L3))
LSum = LSum1 + LSum2 + LSum3 + LSum4 + LSum5 + LSum6 + LSum7
#print
#print "%d=%d+%d+%d+%d+%d+%d+%d" % (len(LSum),len(LSum1),len(LSum2),len(LSum3),len(LSum4),len(LSum5),len(LSum6),len(LSum7))
return LSum
def CountSum(L, total):
LDis=[]
LString=[]
for i in range(len(L)):
if L[i].a % L[i].b == 0:
if L[i].a / L[i].b == total:
LDis.append(L[i])
LString.append(L[i].c)
#remove repeatdata
LString=list(set(LString))
return LString
def SourceCount(L, total):
LObj=[]
for i in range(len(L)):
tempobj=data()
tempobj.a = L[i]
tempobj.b = 1
tempobj.c = "%d" % tempobj.a
LObj.append(tempobj)
L1=[];L2=[];L3=[];L4=[]
L1.append(LObj[0])
L2.append(LObj[1])
L3.append(LObj[2])
L4.append(LObj[3])
LSum=Merge4List(L1,L2,L3,L4)
return CountSum(LSum, total)
if __name__ == "__main__":
L=[]
for i in range(NUMBER):
x=raw_input('entry: ')
L.append(int(x))
LString = SourceCount(L,TOTAL)
for i in range(len(LString)):
print LString[i],'=',TOTAL
print '/n'
raw_input('Press any key to exit...')