题解 | #24点运算#这个题还是有点挑战难度的,需要细心

24点运算

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

from itertools import permutations
d1 = {'J': '11', 'Q': '12', 'K': '13', 'A': '1'}
meth = ['+', '-', '*', '//']
L10 = []
s = input().strip().split()
for i in range(len(s)):
    if s[i] in d1:
        s[i] = d1[s[i]]
# print(s)
if s.count('joker') != 0 or s.count('JOKER') != 0:
    print('ERROR')
else:
    per = permutations(s)
    met = permutations(meth, 3)
    L4 = []
    for i in met:
        L4.append(i)
    L4.append(['+', '+', '+'])       ##组合缺少两种组合符
    L4.append(['*', '*', '*'])
    # print(L4)
    for i in per:
        for j in L4:
            K = 0
            K = eval(i[0]+j[0]+i[1])
            K = eval(str(K)+j[1]+i[2])
            K = eval(str(K)+j[2]+i[3])
            if K == 24:
                L8 = list(i)
                if j[0] == '//':
                    L8.insert(1,'/')
                else:
                    L8.insert(1, j[0])
                if j[1] == '//':
                    L8.insert(3, '/')
                else:
                    L8.insert(3, j[1])
                if j[2] == '//':
                    L8.insert(5, '/')
                else:
                    L8.insert(5, j[2])
#                 print(L8)
                # print(i)
                # print(j)
                for q in range(len(L8)):
                    if L8[q] == '11':
                        L8[q] = 'J'
                    if L8[q] == '12':
                        L8[q] = 'Q'
                    if L8[q] == '13':
                        L8[q] = 'K'
                    if L8[q] == '1':
                        L8[q] = 'A'
#                 print(L8)
                resu = ''.join(L8)
                L10.append(resu)
                break
    if L10 == []:
        print('NONE')
    else:
        print(L10[0])
全部评论

相关推荐

贪食滴🐶:你说熟悉扣篮的底层原理,有过隔扣职业球员的实战经验吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务