24点
1、穷举迭代
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #coding:utf8 2 from __future__ import division 3 import itertools 4 5 def dian(a,b,c,d): 6 num_list = [str(a),str(b),str(c),str(d)] 7 all = [] 8 #遍历所有数字组合的可能性 9 for n in itertools.permutations(num_list,4): 10 all.append(n) 11 12 #遍历所有符号组合的可能性 13 ops = ['+','-','*','/'] 14 ops_list = [[x,y,z] for x in ops for y in ops for z in ops] 15 16 #数字和符号组合 17 z = [] 18 for i in all: 19 for j in ops_list: 20 j.append('') 21 z.append(zip(i,j)) 22 23 #组成表达式 24 biao1 = [] 25 for i in z: 26 s='' 27 for j in i: 28 s+=j[0]+j[1] 29 biao1.append(s) 30 31 #加上第一个( 32 biao2=[] 33 for i in biao1: 34 for j in range(6): 35 g = list(i) 36 g.insert(j,'(') 37 s='' 38 for k in g: 39 s+=k 40 biao2.append(s) 41 42 # 加上第一个) 43 biao3=[] 44 for i in biao2: 45 for j in range(1,7): 46 g = list(i) 47 g.insert(j,')') 48 s='' 49 for k in g: 50 s+=k 51 biao3.append(s) 52 # 加上第二个( 53 biao4=[] 54 for i in biao3: 55 for j in range(6): 56 g = list(i) 57 g.insert(j,'(') 58 s='' 59 for k in g: 60 s+=k 61 biao4.append(s) 62 # 加上第二个) 63 biao5=[] 64 for i in biao4: 65 for j in range(1,7): 66 g = list(i) 67 g.insert(j,')') 68 s='' 69 for k in g: 70 s+=k 71 biao5.append(s) 72 print(len(biao5)) 73 for i in biao5: 74 try: 75 result = eval(i) 76 if result==24: 77 print(i+'=24') 78 except Exception as e: 79 pass 80 81 82 dian(3,9,7,8)
结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 共1990656种 2 ((9)+7-8)*3=24 3 ((9+7)-8)*3=24 4 ((9)+7-8)*3=24 5 ((9+7)-8)*3=24 6 (9+(7)-8)*3=24 7 (9+(7)-8)*3=24 8 ((9)-8+7)*3=24 9 ((9-8)+7)*3=24 10 ((9)-8+7)*3=24 11 ((9-8)+7)*3=24 12 (9-(8)+7)*3=24 13 (9-(8)+7)*3=24 14 ((7)+9-8)*3=24 15 ((7+9)-8)*3=24 16 ((7)+9-8)*3=24 17 ((7+9)-8)*3=24 18 (7+(9)-8)*3=24 19 (7+(9)-8)*3=24 20 ((7)-8+9)*3=24 21 ((7-8)+9)*3=24 22 ((7)-8+9)*3=24 23 ((7-8)+9)*3=24 24 (7-(8)+9)*3=24 25 (7-(8)+9)*3=24 26 共耗时14.0639998913秒
最low也是最简单的方式,迭代了1990656种,耗时14.0639998913秒
优化一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #coding:utf8 2 from __future__ import division 3 import itertools 4 import time 5 6 def dian(a,b,c,d): 7 num_list = [str(a),str(b),str(c),str(d)] 8 all = [] 9 #遍历所有数字组合的可能性 10 for n in itertools.permutations(num_list,4): 11 all.append(n) 12 13 #遍历所有符号组合的可能性 14 ops = ['+','-','*','/'] 15 ops_list = [[x,y,z] for x in ops for y in ops for z in ops] 16 17 #数字和符号组合 18 z = [] 19 for i in all: 20 for j in ops_list: 21 j.append('') 22 z.append(zip(i,j)) 23 24 fmt = [ 25 '{A}{x}{B}{y}{C}{z}{D}', 26 '{A}{x}(({B}{y}{C}){z}{D})', 27 '(({A}{x}{B}){y}{C}){z}{D}', 28 '({A}{x}{B}){y}({C}{z}{D})', 29 '({A}{x}({B}{y}{C})){z}{D}', 30 '{A}{x}({B}{y}({C}{z}{D}))' 31 ] 32 33 for i in z: 34 for f in fmt: 35 f1 = f.format(A=i[0][0],x=i[0][1],B=i[1][0],y=i[1][1],C=i[2][0],z=i[2][1],D=i[3][0]) 36 try: 37 if eval(f1) == 24: 38 print(f1+'=24') 39 except Exception as e: 40 pass 41 42 t1 = time.time() 43 dian(5,6,7,8) 44 t2 =time.time() 45 print(t2-t1)
结果:
手工列举了括号的排列方式,使得迭代的数量和循环的数量大减,迭代了9216种,耗时0.111000061035秒
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ((5+7)-8)*6=24 2 (5+(7-8))*6=24 3 (5+7)*(8-6)=24 4 ((5-8)+7)*6=24 5 (5-(8-7))*6=24 6 6*((5+7)-8)=24 7 6*(5+(7-8))=24 8 6*((5-8)+7)=24 9 6*(5-(8-7))=24 10 6*((7+5)-8)=24 11 6*(7+(5-8))=24 12 (6/(7-5))*8=24 13 6/((7-5)/8)=24 14 6*((7-8)+5)=24 15 6*(7-(8-5))=24 16 (6*8)/(7-5)=24 17 6*(8/(7-5))=24 18 ((7+5)-8)*6=24 19 (7+(5-8))*6=24 20 (7+5)*(8-6)=24 21 ((7-8)+5)*6=24 22 (7-(8-5))*6=24 23 (8-6)*(5+7)=24 24 (8-6)*(7+5)=24 25 (8*6)/(7-5)=24 26 8*(6/(7-5))=24 27 (8/(7-5))*6=24 28 8/((7-5)/6)=24 29 耗时0.111000061035秒
总结:
效率不错,但是依赖语言特性,比如intertools和eval
2、二叉树递归