题解 | 穷举#24点运算#

24点运算

http://www.nowcoder.com/practice/7e124483271e4c979a82eb2956544f9d

import itertools
def change_str(str_cal):
    str_cal = str_cal.replace('11','J').replace('12','Q').replace('13','K').replace('1','A')
    return str_cal
def check_str():
    num =[]
    cal = ['+','-','*','/']
    for t in s:
        if t == 'JOKER' or t == 'joker':
            return "ERROR"
        elif t in lst1:
            num.append(lst2[lst1.index(t)])
        else:
            num.append(t)
    cal_comb = list(itertools.product(cal,repeat=3))
    num_comb = list(itertools.permutations(num,4))
    cn_comb = []
    for m in num_comb:
        for n in cal_comb:
            new_cal = m[0]
            str_cal = m[0]
            for i in range(3):
                new_cal = str(int(eval(new_cal+n[i]+m[i+1])))
                str_cal += n[i] + m[i+1]
            if new_cal == '24':
                return change_str(str_cal)
    else:
        return "NONE"


while True:
    try:
        s= input().split(" ")
        lst1 = "JQKA"
        lst2 = ['11','12','13','1']
        print(check_str())
    except:
        break
全部评论

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
去B座二楼砸水泥地:不过也可以理解,这种应该没参加过秋招
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务