题解 | #24点运算#

24点运算

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

import copy

poker_value = {'J':'11', 'Q':'12', 'K':'13', 'A':'1'}
value_poker = {11:'J', 12:'Q', 13:'K', 1:'A'}
op = [[0, 0], [0, 1], [1, 0], [1, 1]]

def op_symbol(operation):
    if operation == [1, 0]:
        return '+'
    elif operation == [1, 1]:
        return '-'
    elif operation == [0, 0]:
        return '*'
    else:
        return '/'
    
def num2poker(num):
    if 2 <= num <= 10:
        return str(num)
    else:
        return value_poker[num]


def cal(num1, num2, operation):
    if operation == [1, 0]:
        return num1+num2
    elif operation == [1, 1]:
        return num1-num2
    elif operation == [0, 0]:
        return num1*num2
    else:
        return num1//num2

fetched = input().split(' ')
# fetched = ['4', '2', 'K', 'A']
fetched_1 = copy.deepcopy(fetched)
if 'joker' in fetched or 'JOKER' in fetched:
    print('ERROR')
else:
    for i, v in enumerate(fetched):
        if v in poker_value.keys():
            fetched[i] = poker_value[v]

    fetched_2 = list(map(int, fetched))

    game_win = 0
    for i in op:
        for j in op:
            for k in op:
                candidates = copy.deepcopy(fetched_2)
                for ii in range(len(candidates)):
                    c1 = candidates[ii]*1
                    candidates_l1 = copy.deepcopy(candidates)
                    del candidates_l1[ii]
                    for jj in range(len(candidates_l1)):
                        c2 = candidates_l1[jj]*1
                        candidates_l2 = copy.deepcopy(candidates_l1)
                        del candidates_l2[jj]
                        for mm in range(len(candidates_l2)):
                            c3 = candidates_l2[mm]*1
                            candidates_l3 = copy.deepcopy(candidates_l2)
                            del candidates_l3[mm]
                            c4 = candidates_l3[0]*1
                            fetched = [c1, c2, c3, c4]
                            fetched_3 = [c1, c2, c3, c4]
                            ops = [[0, i], [1, j], [2, k]]
                            for n, o in enumerate(ops):
                                # print(i ,j, k, ii, jj, mm, fetched, ops, o[0])
                                fetched[o[0]] = cal(fetched[o[0]], fetched[o[0]+1], o[1])
                                del fetched[o[0]+1]
                                for m in range(len(ops[n:])):
                                    ops[n+m][0] -= 1
                        
                            if fetched[0] == 24:
                                print(num2poker(fetched_3[0])+op_symbol(ops[0][1])+num2poker(fetched_3[1])+op_symbol(ops[1][1])+num2poker(fetched_3[2])+op_symbol(ops[2][1])+num2poker(fetched_3[3]))
                                game_win = 1
                                break
                        if game_win: break
                    if game_win: break
                if game_win: break
            if game_win: break
        if game_win: break
    if not game_win:
        print('NONE')

暴力解法,列举所有的操作符排列组合,列举所有的操作数排列组合

  1. 操作数的排列组合写法,每一层循环代表在当前位置的所有可能值,下一层循环中临时删掉这个值,以此类推
  2. 从左至右运算写法,维护一个操作数列表,运算完一次就把列表的前两个元素合并为运算结果,同时操作符列表后续的索引号减一(为了对应操作数列表的长度变化)

期初以为要考虑四则运算顺序,方法2也适用,首先对操作符进行排序,在把中间的两个操作数合并为运算结果后,在操作符列表中,位置索引大于当前值的值减一

全部评论

相关推荐

程序员鼠鼠_春招版:都很烂大街,rpc也基本没人问,考研吧,不然就包装一段实习再去
点赞 评论 收藏
分享
头像
02-15 16:23
中南大学 Java
野猪不是猪🐗:签了美团真是不一样! 亲戚们都知道我签了美团,过年都围着我问送一单多少钱,还让弟弟妹妹们引以为戒,笑我爸我妈养了个🐢孩子,说从小就知道我这个人以后肯定没出息,我被骂的都快上天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务