|
发表于 2018-1-25 09:01:01
|
显示全部楼层
贴上我的比较粗糙的代码,上半部分是打散13个数的排列,再依次挑出4个数,后面是不停变换4个数排序,再依次添加上运算符号,最后转化为运算式,感觉写的很粗糙,我再研究下版主代码
- import numpy as np
- import itertools as intr
- t = [1,2,3,4,5,6,7,8,9,10,11,12,13]
- n = 20
- while n:
- np.random.shuffle(t)
- kapai = intr.permutations(t,4)
- for i in kapai:
- input('开始发牌')
- temp = list(i)
- break
- s = ['*','/','+','-']
- list_count = []
- print(temp)
- #temp = [4,5,2,5]
- xunhuan = 24
- input('是否求解')
- print(' ')
- while xunhuan:
- np.random.shuffle(temp)
- for j in intr.combinations_with_replacement(s,3):
- m = '('+str(temp[0])+j[0]+str(temp[1])+')'+j[1]+'('+str(temp[2])+j[2]+str(temp[3])+')'
- count = eval(m)
- if count == 24:
- print('找到了——————————————————————————')
- print(m)
- break
- m = '('+'('+str(temp[0])+j[0]+str(temp[1])+')'+j[1]+str(temp[2])+')'+j[2]+str(temp[3])
- count = eval(m)
- if count == 24:
- print('找到了——————————————————————————')
- print(m)
- break
- m = str(temp[0])+j[0]+'('+str(temp[1])+j[1]+str(temp[2])+')'+j[2]+str(temp[3])
- count = eval(m)
- if count == 24:
- print('找到了——————————————————————————')
- print(m)
- break
- try:
- m = str(temp[0])+j[0]+'('+'('+str(temp[1])+j[1]+str(temp[2])+')'+j[2]+str(temp[3])+')'
- count = eval(m)
- if count == 24:
- print('找到了——————————————————————————')
- print(m)
- break
- except:
- continue
-
- if count == 24:
- break
- xunhuan -= 1
- else:
- print('不能组成24点')
- print(' ')
-
-
-
-
-
-
- print('╭~~~╮')
- print('(o~.~o)')
- n -= 1
复制代码 |
|