使用Python创建24点题库和答案库
24点游戏的4个数并不是乱出的,它必须符合一定的条件才能计算得出24,否则题目无解。
本文使用Python编写一个生成24点题库,希望能枚举所有小于某个最大数以内的所有可能解。
代码如下:
# 24 point library creater ops={'0'} ops.discard('0') op = ['+', '-', '*', '/'] for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('A' +op[i] +'B' +op[j] +'C'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('(A' +op[i] +'B)' +op[j] +'C'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('A' +op[i] +'(B' +op[j] +'C)'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('(A' +op[i] +'B)' +op[j] +'(C'+ op[k]+ 'D)') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('(A' +op[i] +'B' +op[j] +'C)'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('A' +op[i] +'(B' +op[j] +'C'+ op[k]+ 'D)') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('((A' +op[i] +'B)' +op[j] +'C)'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('(A' +op[i] +'(B' +op[j] +'C))'+ op[k]+ 'D') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('A' +op[i] +'(B' +op[j] +'(C'+ op[k]+ 'D))') for i in range(0,4): for j in range(0,4): for k in range(0,4): ops.add('A' +op[i] +'((B' +op[j] +'C)'+ op[k]+ 'D)') pos={'0123'} for i in range(0,4): for j in range(0,4): if j==i: continue for k in range(0,4): if (k==i) or (k == j): continue for l in range(0,4): if (l==i) or (l==j) or (l==k): continue pos.add(str(i)+str(j)+str(k)+str(l)) opsall={'0'} opsall.discard('0') Alpha='ABCD' alpha='abcd' for ipos in pos: for iops in ops: s = iops for kk in range(0,4): s = s.replace(Alpha[kk], alpha[int(ipos[kk])]) opsall.add(s) numlib={''} numlib.discard('') reslib={''} reslib.discard('') count=0 maxnum = 13 filehandle = open("G:\out.txt", "a") tcount=maxnum*maxnum*maxnum; curcnt=0; for a in range(1,maxnum+1): for b in range(1,maxnum+1): for c in range(1,maxnum+1): curcnt=curcnt+1; r = float(curcnt)*100/tcount; print('step ('+str(curcnt) +'/' + str(tcount) + ', ' + '{:.2f}'.format(r) + '%) ' + str(a)+'.'+str(b)+'.'+str(c)) for d in range(1,maxnum+1): for itemop in opsall: try: o=itemop if o.find('/')>=0: o=o.replace('a', 'AA') o=o.replace('b', 'float(b)') o=o.replace('c', 'float(c)') o=o.replace('d', 'float(d)') o=o.replace('AA', 'a') res = eval(o) if res==24: arr=[a,b,c,d] arr.sort() num = ','.join(str(ii) for ii in arr) s=itemop s=s.replace('a', str(a)) s=s.replace('b', str(b)) s=s.replace('c', str(c)) s=s.replace('d', str(d)) if num not in numlib: numlib.add(num) xx=num + '; ' + s + ' = 24' reslib.add(xx) count=count+1 line=str(count)+": " + xx + "\n" filehandle.write(line) break except: continue else: continue print('Found count: ' + str(count)) filehandle.close() for x in reslib: print(x)
比如,当这个最大数为7时,输出结果如下,一共170种组合方式。
Found count: 170
1,3,4,6; 6/(1-3/4) = 24
2,2,5,7; 7*2+5*2 = 24
3,5,6,6; (3+6-5)*6 = 24
2,2,7,7; 2*(7-2+7) = 24
2,4,4,5; (5*2-4)*4 = 24
2,3,5,6; 3*2*5-6 = 24
2,2,3,3; (2+2)*(3+3) = 24
1,2,2,4; (2*4)*(1+2) = 24
1,2,3,5; (1+2)*(3+5) = 24
1,4,7,7; (7-4)*(1+7) = 24
3,3,3,4; (4+3)*3+3 = 24
2,3,7,7; (7+7*2)+3 = 24
1,1,6,6; 6*(6-1-1) = 24
5,6,6,6; 6*(5-6/6) = 24
1,1,3,5; (5+1)*(3+1) = 24
1,1,5,6; (5-1)*6*1 = 24
1,2,4,6; (4-1)*(6+2) = 24
3,4,5,7; (5+7)+4*3 = 24
4,4,4,5; (4/4+5)*4 =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-3+4) = 24
1,1,4,7; (4*1)*(7-1) = 24
4,6,6,7; (7-6/6)*4 = 24
4,5,5,5; (4+5*5)-5 = 24
3,4,7,7; (4*7+3)-7 = 24
3,3,3,3; (3*3)*3-3 = 24
3,4,4,4; (3+4)*4-4 = 24
1,1,4,4; (4+1+1)*4 = 24
1,1,3,7; (7+1)/(1/3) = 24
1,4,6,6; (1+4)*6-6 = 24
1,3,4,7; (7*4-1)-3 = 24
1,3,7,7; (3-7)*(1-7) = 24
4,6,6,6; (6*4)*6/6 = 24
最大值13以内的题目如下: