其他分享
首页 > 其他分享> > 牛客华为机试HJ89

牛客华为机试HJ89

作者:互联网

原题传送门

1. 问题描述

2. Solution

import sys
from itertools import permutations, product

if sys.platform != "linux":
    file_in = open("input/HJ89.txt")
    sys.stdin = file_in

num_map = dict(zip(
    "3 4 5 6 7 8 9 10 J Q K A 2".split(),
    [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2]
))

calc = dict(zip(
    ['+', '-', '*', '/'],
    [lambda x, y: x + y, lambda x, y: x - y, lambda x, y: x * y, lambda x, y: x // y]
))


def solve(cards):
    if 'joker' in cards:
        return 'ERROR'
    for a1, a2, a3, a4 in permutations(cards):
        for c1, c2, c3 in product(calc, repeat=3):
            res1 = calc[c1](num_map[a1], num_map[a2])
            res2 = calc[c2](res1, num_map[a3])
            res3 = calc[c3](res2, num_map[a4])
            if res3 == 24:
                return f'{a1}{c1}{a2}{c2}{a3}{c3}{a4}'
    return "NONE"


for line in sys.stdin:
    cards = line.strip().split()
    res = solve(cards)
    print(res)

标签:map,sys,牛客,num,HJ89,cards,机试,calc,lambda
来源: https://www.cnblogs.com/junstat/p/16177308.html