题解 | #24点游戏算法#
24点游戏算法
http://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
时间有点长,但是效果还是不错的
import itertools while True: try: nums_list = list(map(int,input().split(' '))) n = len(nums_list) order = ['+','-','*','/'] for nums in itertools.permutations(nums_list): for i in order: for j in order: for k in order: result = eval('{0}{1}{2}'.format(nums[0],i,nums[1])) result = eval('{0}{1}{2}'.format(result,j,nums[2])) result = eval('{0}{1}{2}'.format(result,k,nums[3])) if result == 24: print("true") raise ValueError else: print('false') except: break