Python题解 | #24点运算#
24点运算
https://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d
import sys from itertools import permutations card = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] order = range(1, 14) card_order = dict(zip(card, order)) opts = ['+', '-', '*', '/'] def cal(a1, a2, opt): if opt == 0: return a1 + a2 elif opt == 1: return a1 - a2 elif opt == 2: return a1 * a2 elif opt == 3: return a1 / a2 def cal24(cards): if 'joker' in cards or 'JOKER' in cards: print('ERROR') return num_orders = permutations(cards) for num in num_orders: for i in range(4): for j in range(4): for k in range(4): a = cal(card_order[num[0]], card_order[num[1]], i) b = cal(a, card_order[num[2]], j) c = cal(b, card_order[num[3]], k) if c == 24: print(f"{num[0]}{opts[i]}{num[1]}{opts[j]}{num[2]}{opts[k]}{num[3]}") return print('NONE') return while True: try: cards = input().split(' ') cal24(cards) except: break