java2python 发表于 2020-7-9 16:13:54

python实现24点游戏(地球上最短的24点游戏代码?)

牛bi要挑大的吹,否则看帖的人没几个(这个标题纯属吹牛。。。)

所谓24点游戏,就是4个数字和算符的排列组合:

四个里挑两个P(4,2) 计算:四个算符
四个里剩余两个+计算结果,三个里挑两个P(3,2) 计算:四个算符
经过上一轮3个里剩余1个+计算结果,两个里挑两个(排列)计算:四个算符
所以,全部可能性:P(4,2)*4*P(3,2)*4*P(2,2)*4=9216(只可能有重复,不会有逃脱)

实现:
*首先需要N个里取几个的排列函数
*其次就是得到24点公式的函数
*初始化(4个取2个,3个取2个,2个取2个),以及算符
*最后是所谓的难局调用(改了,全部715局,其中566局有解)

def P(n,c, repeat=False,com=0,limit=0,per=[]):
    for pos in range(limit,n):
      t = per +
      if repeat or len(set(t)) == len(t):
            if len(t) == c:
                  yield
            else:
                  for result in P(n,c,repeat,com,com*pos, per + ):
                            yield + result

def get_formula(l0):
    for r in res:
      for o in operator:
            formula = '(%s%s%s)' % (l0],o,l0])
            if len(l0) == 2:
                try:
                  if abs(eval(formula)-24)<0.1: return '%s%s%s' % (l0],o,l0])
                except:
                  pass
            else:
                l1 = for x in range(len(l0)) if not(x in r)] +
                f = get_formula(l1)
                if f != None:
                  return f
    return None

res = (,,)
operator = ('+','-','*','/')

for r0 in P(11,4,True,1,1):
    print(r0,get_formula())

结果(有几百行):
None
None
None
None
None
None
None
8*(1+(1+1))
None
None
None
None
None
None
(1+1)*(2*6)
(1+2)*(1+7)
8*(2+(1*1))
(1+2)*(9-1)
(1+1)*(2+10)
None
(1+1)*(3*4)
(1+3)*(1+5)
3*(6+(1+1))
3*(7+(1*1))
(1-1)+(3*8)
(1+1)*(3+9)
3*(10-(1+1))
4*(4+(1+1))
4*(5+(1*1))
(1-1)+(4*6)
4*(7-(1*1))
(1+1)*(4+8)
(1-4)*(1-9)
4+(10*(1+1))
(5*5)-(1*1)
6*(5-(1*1))
(1+1)*(5+7)
8*(5-(1+1))
None
None
(1+1)*(6+6)
None
(6*8)/(1+1)
6+(9*(1+1))
None
None
None
None
10+(7*(1+1))
8+(8*(1+1))
None
None
None
None
None
None
None
(1+2)*(2*4)
(1+5)*(2+2)
(1+2)*(2+6)
(2+2)*(7-1)
8*(2-(1-2))
2*(9+(1+2))
(1+2)*(10-2)
(1+3)*(2*3)
4*(3+(1+2))
(1+2)*(3+5)
6*(3-(1-2))
(1+2)+(3*7)
(1+3)*(8-2)
(3*9)-(1+2)
3*(10-(1*2))
(1+2)*(4+4)
4*(5-(1-2))
(2-1)*(4*6)
4*(7+(1-2))
8*(4+(1-2))
4*(9-(1+2))
4+(10*(1*2))
(1-2)+(5*5)
6*(5+(1-2))
(1*2)*(5+7)
8*(5-(1*2))
9+(5*(1+2))
(2*10)-(1-5)
6+(6*(1+2))
6*(7-(1+2))
8*(6-(1+2))
6+(9*(1*2))
(10*(1+2))-6
((7*7)-1)/2
(1+7)+(2*8)
(2*9)-(1-7)
10+(7*(1*2))
8+(8*(1*2))
(8*9)/(1+2)
10-(2*(1-8))
None
None
None
(1+3)*(3+3)
3*(4+(1+3))
(1*3)*(3+5)
3*(6-(1-3))
(1*3)+(3*7)
(3*(1+8))-3
(1+3)*(9-3)
3*(10+(1-3))
4*(4-(1-3))
(1+3)+(4*5)
6/(1-(3/4))
(4*7)-(1+3)
8+(4*(1+3))
4*(9-(1*3))
(1+3)*(10-4)
None
(1+5)+(3*6)
(1+5)*(7-3)
8*(5+(1-3))
9+(5*(1*3))
(3*10)-(1+5)
6*(6+(1-3))
6*(7-(1*3))
6*(8-(1+3))
6-(9*(1-3))
(10*(1*3))-6
(1-7)*(3-7)
8*(7-(1+3))
(1+7)/(3/9)
10-(7*(1-3))
(8*(1+3))-8
(8*9)/(1*3)
(10-1)/(3/8)
(9-1)/(3/9)
(3*(1+10))-9
(1+3)+(10+10)
4+(4*(1+4))
(1*4)+(4*5)
(4*(1+6))-4
(4*7)-(1*4)
8+(4*(1*4))
4*(9+(1-4))
(1*4)*(10-4)
(4*5)-(1-5)
6/((5/4)-1)
(1-5)+(4*7)
(1+5)*(8-4)
9-(5*(1-4))
(1-5)*(4-10)
(6*(1+4))-6
6*(7+(1-4))
8*(6+(1-4))
6*(9-(1+4))
(10*(4-1))-6
(1+7)*(7-4)
8*(7-(1*4))
(1-9)*(4-7)
None
8*(8-(1+4))
(1-9)+(4*8)
None
None
(1+4)+(9+10)
(1*4)+(10+10)
5*(5-(1/5))
(5*6)-(1+5)
None
None
(1+5)*(9-5)
(5*(10-5))-1
(6*(1*5))-6
(1-7)+(5*6)
6*(8+(1-5))
6*(9-(1*5))
(1+5)*(10-6)
None
8*(7+(1-5))
(1-7)*(5-9)
(5*7)-(1+10)
8*(8-(1*5))
8*(9-(1+5))
(1+5)+(8+10)
(1+5)+(9+9)
(1*5)+(9+10)
(10+10)-(1-5)
(6*(6-1))-6
None
6/(1-(6/8))
6*(9+(1-6))
(1*6)*(10-6)
None
None
(1+7)*(9-6)
(1+6)+(7+10)
8*(8+(1-6))
(1+6)+(8+9)
8*(10-(1+6))
(1*6)+(9+9)
(9+10)-(1-6)
None
None
None
(1+7)+(7+9)
(1+7)*(10-7)
(1+7)+(8+8)
8*(9+(1-7))
(8+10)-(1-7)
(9+9)-(1-7)
(1-9)*(7-10)
None
(1*8)+(8+8)
(8+9)-(1-8)
8*(10+(1-8))
None
None
None
None
None
None
None
None
(2+2)*(2*3)
(2+2)*(2+4)
2*(2+(2*5))
None
2*((2*7)-2)
(2+2)*(8-2)
2+(2*(2+9))
(2+2)+(2*10)
(2+2)*(3+3)
3*(4+(2+2))
3*((2*5)-2)
6*(3+(2/2))
3*(7+(2/2))
(2-2)+(3*8)
(2+2)*(9-3)
(2*(3+10))-2
2*(4+(2*4))
(2+2)+(4*5)
(2-2)+(4*6)
(4*7)-(2+2)
8+(4*(2+2))
(2+4)+(2*9)
(2+2)*(10-4)
(5*5)-(2/2)
6*(5-(2/2))
(2*5)+(2*7)
(2*(5+8))-2
2*(9-(2-5))
10+(2*(2+5))
(2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10)
(5*8)-(2*8)
(2+5)+(8+9)
8*(10-(2+5))
None
(2*10)-(5-9)
(2+10)/(5/10)
(2*6)+(6+6)
6*(7-(6/2))
6*(8+(2-6))
(2+6)*(9-6)
(2+6)+(6+10)
None
8*(7+(2-6))
(2+6)+(7+9)
(2+6)*(10-7)
(2+6)+(8+8)
8*((2*6)-9)
(2*6)*(10-8)
2*(9-(6-9))
(2-10)*(6-9)
(10+10)-(2-6)
None
(2+7)+(7+8)
None
7*(2+(10/7))
8*(8+(2-7))
(2*(7+9))-8
None
None
(9+10)-(2-7)
(2-10)*(7-10)
(8/(2/8))-8
8*(9+(2-8))
(8+10)-(2-8)
(9+9)-(2-8)
8*(10+(2-9))
(2+10)*(10-8)
None
None
(9+10)+(10/2)
None
(3*(3*3))-3
4*((3*3)-3)
(3*3)+(3*5)
(3+3)+(3*6)
(3+3)*(7-3)
8*((3+3)-3)
3*(9-(3/3))
(3*10)-(3+3)
(3*4)+(3*4)
4*(5+(3/3))
(3-3)+(4*6)
4*(7-(3/3))
(3+3)*(8-4)
3*(9+(3-4))
None
(5*5)-(3/3)
(5*6)-(3+3)
3*((3*5)-7)
None
(3+3)*(9-5)
(3*3)+(5+10)
3*(6+(6/3))
(3*7)-(3-6)
8*((3*3)-6)
(3*3)+(6+9)
(3+3)*(10-6)
7*(3+(3/7))
(3*3)+(7+8)
(3-7)*(3-9)
None
8/(3-(8/3))
8*(9-(3+3))
(3+3)+(8+10)
(3+3)+(9+9)
(3-9)+(3*10)
None
(4*(3+4))-4
4*(5-(3-4))
(3*4)*(6-4)
4*(7+(3-4))
8*((3+4)-4)
(4*9)-(3*4)
(4*(10-3))-4
(3-4)+(5*5)
6*(5+(3-4))
(3*4)+(5+7)
(4*8)-(3+5)
4*((3*5)-9)
(3*4)/(5/10)
(3*4)+(6+6)
None
(3*4)*(8-6)
4*(9+(3-6))
(3*6)-(4-10)
(3-7)+(4*7)
8-(4*(3-7))
(3*4)*(9-7)
(3+4)+(7+10)
None
(3+4)+(8+9)
8*(10-(3+4))
(9+9)/(3/4)
None
(3*10)+(4-10)
None
(3*(5+5))-6
(5-3)*(5+7)
(3+5)*(8-5)
3*(9-(5/5))
None
6*(6+(3-5))
(5+7)/(3/6)
(3*8)*(6-5)
(3+5)*(9-6)
(3+5)+(6+10)
None
(3*7)-(5-8)
(3+5)+(7+9)
(3+5)*(10-7)
(3+5)+(8+8)
(3*9)+(5-8)
None
9+(9/(3/5))
(3+9)/(5/10)
3*(10-(10/5))
6-(6*(3-6))
6*(7+(3-6))
8*((3+6)-6)
(3+6)+(6+9)
(10*(6-3))-6
3*(7-(6-7))
(3+6)+(7+8)
(3*7)-(6-9)
10+(7/(3/6))
8+(8/(3/6))
6*(9+(3-8))
3*(10+(6-8))
(3*9)+(6-9)
(3-9)*(6-10)
10*(3-(6/10))
(3+7)+(7+7)
8*((3+7)-7)
3*(9-(7/7))
(3*7)-(7-10)
(3*8)*(8-7)
3*(9+(7-8))
None
(3+9)*(9-7)
(3*9)+(7-10)
(10+10)-(3-7)
8*((3+8)-8)
(3*8)*(9-8)
((8*10)-8)/3
(3*8)+(9-9)
(9+10)-(3-8)
(3*8)+(10-10)
(9+9)-(3-9)
3*((9+9)-10)
3*(9-(10/10))
None
(4+4)+(4*4)
4*(5+(4/4))
6*((4+4)-4)
(4+4)*(7-4)
(4*8)-(4+4)
4-(4*(4-9))
(4*10)-(4*4)
(5*5)-(4/4)
6*(5-(4/4))
4*(7+(4-5))
(4+4)*(8-5)
None
4-(4*(5-10))
None
None
4*(8+(4-6))
(4+4)*(9-6)
(4+4)+(6+10)
7*(4-(4/7))
(4*7)+(4-8)
(4+4)+(7+9)
(4+4)*(10-7)
(4+4)+(8+8)
(4*9)-(4+8)
(4-8)*(4-10)
None
None
((10*10)-4)/4
(4-5)+(5*5)
6*((4+5)-5)
4*(7-(5/5))
8*(4-(5/5))
(4*5)-(5-9)
(4+5)+(5+10)
(4*6)*(6-5)
4*(7+(5-6))
8*((4+5)-6)
(4+5)+(6+9)
(4*5)-(6-10)
(5*7)-(4+7)
(4+5)+(7+8)
9-(5*(4-7))
4-(10*(5-7))
8*(5-(8/4))
4*(9+(5-8))
(4+8)/(5/10)
4*(5+(9/9))
(4-10)*(5-9)
4+(10/(5/10))
6*((4+6)-6)
(4*6)*(7-6)
(4+6)+(6+8)
6-(9*(4-6))
(6+10)/(4/6)
(4+6)+(7+7)
8*((4+6)-7)
(7+9)/(4/6)
10-(7*(4-6))
8-(8*(4-6))
(4*6)*(9-8)
4-(10*(6-8))
(4*6)+(9-9)
(4*6)*(10-9)
(4*6)+(10-10)
4*(7-(7/7))
4*((7+7)-8)
None
None
8*((4+7)-8)
(4+8)*(9-7)
10+(8/(4/7))
4*(7-(9/9))
(4*10)-(7+9)
4*(7-(10/10))
8+(8/(4/8))
8*((4+8)-9)
(4+8)*(10-8)
8*(4-(9/9))
8*((4+9)-10)
(10+10)-(4-8)
None
(9+10)-(4-9)
None
None
(5*5)-(5/5)
(5*5)+(5-6)
None
None
(5+5)+(5+9)
None
6*((5+5)-6)
(5*5)+(6-7)
(5+5)+(6+8)
None
None
(5+5)+(7+7)
8*((5+5)-7)
None
(5+7)/(5/10)
(5*5)-(8/8)
(5*5)+(8-9)
(5+10)/(5/8)
(5*5)-(9/9)
(5*5)+(9-10)
(5*5)-(10/10)
6*(5-(6/6))
(5+6)+(6+7)
6-(6*(5-8))
(6*9)-(5*6)
(6+6)/(5/10)
6*(5-(7/7))
(5+7)*(8-6)
6-(9*(5-7))
None
8*((5+6)-8)
6*((5+8)-9)
(5*6)*(8/10)
9-(5*(6-9))
6*((5+9)-10)
(10+10)/(5/6)
None
None
(5+7)*(9-7)
10-(7*(5-7))
8-(8*(5-7))
8*((5+7)-9)
(5+7)*(10-8)
None
9-(5*(7-10))
10+(10/(5/7))
(5*8)-(8+8)
(8*9)/(8-5)
8*((5+8)-10)
None
None
None
None
None
(10+10)-(5-9)
None
(6+6)+(6+6)
None
(6+6)*(8-6)
(6*6)*(6/9)
(6*10)-(6*6)
None
None
(6+6)*(9-7)
6-(6*(7-10))
(6*8)/(8-6)
8*((6+6)-9)
(6+6)*(10-8)
None
(6+10)/(6/9)
None
None
None
None
6*((7+7)-10)
None
(6*8)/(9-7)
8*((6+7)-10)
(6*7)-(9+9)
None
(10*(10-7))-6
8-(8*(6-8))
(8*9)-(6*8)
(6*8)/(10-8)
(9+9)/(6/8)
6-(9*(8-10))
None
None
9+(10/(6/9))
None
(10+10)-(6-10)
None
None
None
None
None
None
None
None
10-(7*(7-9))
None
None
8-(8*(7-9))
(8*10)-(7*8)
None
(8*9)/(10-7)
10-(7*(8-10))
None
None
None
None
None
None
8-(8*(8-10))
None
None
None
None
None
None
None
None
None
None
None
None

Twilight6 发表于 2020-7-9 16:54:31

{:7_146:}
页: [1]
查看完整版本: python实现24点游戏(地球上最短的24点游戏代码?)