题解 | #24点游戏算法#
24点游戏算法
https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb
import sys for line in sys.stdin: nums = line.strip().split() ops = ['+', '-', '*', '//'] flag = False def calc_nums(nums, res=0): for i in nums[:]: # must consider the order of those items # print(nums, i) nums.remove(i) num = i # print(nums, res) for op in ops: # print(op) for j in nums: # print(num, op, j) if op == '//' and j == '0': # print('jumped') break res = int(eval(num + op + j)) # print(res) if res == 24: global flag flag = True elif nums: new_nums = nums[:] new_nums.append(str(res)) new_nums.remove(j) calc_nums(new_nums, res) calc_nums(nums) print('true') if flag else print('false')