题解 | #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(lis,a,s):
    if not lis and a==24:
        res.append(s)
    elif len(lis)==4:
        l = []
        for i in range(len(lis)):
            l = lis[:i]+lis[i+1:]
            c = dic[lis[i]]
            fun(l,c,s+lis[i])
    else:
        ll = []
        for i in range(len(lis)):
            ll = lis[:i]+lis[i+1:]
            b = dic[lis[i]]
            fun(ll,a+b,s+'+'+lis[i])
            fun(ll,a-b,s+'-'+lis[i])
            fun(ll,a*b,s+'*'+lis[i])
            fun(ll,a//b,s+'/'+lis[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])

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务