题解 | 穷举#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
全部评论

相关推荐

专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
09-25 10:34
东北大学 Java
多面手的小八想要自然醒:所以读这么多年到头来成为时代车轮底下的一粒尘
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务