题解 | #24点运算#

24点运算

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

dic = {'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':11,'Q':12,'K':13}
def fun(pai,tag,s):
    if not pai:
        if tag==24:
            res.append(s)
    elif len(pai)==4:
        for i in range(len(pai)):
            fun(pai[:i]+pai[i+1:],dic[pai[i]],s+pai[i])
    else:
        l = []
        for i in range(len(pai)):
            l = pai[:i]+pai[i+1:]
            fun(l,tag+dic[pai[i]],s+'+'+pai[i])
            fun(l,tag-dic[pai[i]],s+'-'+pai[i])
            fun(l,tag*dic[pai[i]],s+'*'+pai[i])
            fun(l,tag//dic[pai[i]],s+'/'+pai[i])


lis = input().split()
res = []
if 'joker' in lis or 'JOKER' in lis:
    print('ERROR')
else:
    fun(lis,0,'')
    if not res:
        print('NONE')
    else:
        print(res[0])

全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务