zoukankan      html  css  js  c++  java
  • 华为机试题 24点

    计算24点

    1、描述

           计算24点是一种扑克牌益智游戏,随机抽出4张扑克牌,通过加(+),减(-),乘(*), (/)四种运算法则计算得到整数24

    本问题中,扑克牌通过如下字符或者字符串表示,其中,小写joker表示小王,大写JOKER表示大王:

            3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER

            本程序要求实现:输入4张牌,输出一个算式,算式的结果为24点。

            题目地址牛客网地址

    2、解题思路

           暴力法穷举所有的可能的组合(题目只需找到任意满足24点的式子即可)。

           4 x 4 x 3 x 4 x 2 x 4 x 1= 256 x 6 = 1536 (每层需查找4种算符)

          主要难点是7层循环内部的剪枝,即候选的数字的剪枝,内层的数字不要和外层重复

         (抽出来的4张牌可能重复,即牌面的数值会重复,而牌不能重复-对应位置的牌在式子中只出现一次

    3、代码

    pokers = input().split(" ")
    
    pok_2_digit = {'J': 11, 'Q': 12, 'K': 13, 'A': 1}
    digit_2_pok = {pok_2_digit[k]: k for k in pok_2_digit}
    
    digits = []
    flag = False
    for pok in pokers:
        if len(pok) != 5:
            if pok.isdigit():
                digits.append(int(pok))
            else:
                digits.append(pok_2_digit[pok])
        else:
            flag = True
            break
    
    if flag == True:
        print('ERROR')
    else:
        def operate(nums, opers):
            res = nums[0]
            for i in range(len(opers)):
                num = nums[i+1]
                oper = opers[i]
                if oper == '+':
                    res += num
                elif oper == '-':
                    res -= num
                elif oper == '*':
                    res *= num
                elif oper == '/':
                    res = res // num
            return res
    
        def output(res_nums, res_opers):
            nums = []
            for num in res_nums:
                if num in digit_2_pok:
                    nums.append(digit_2_pok[num])
                else:
                    nums.append(str(num))
    
            res = [nums[0]]
            for i in range(len(res_opers)):
                res.append(res_opers[i])
                res.append(nums[i+1])
            return "".join(res)
    
        opers = ['+', '-', '*', '/']
        def check_24(digits, opers):
            n = len(digits)
            for i1 in range(n):
                d1 = digits[i1]
                for o1 in opers:
                    for i2 in range(n):
                        if i2 != i1:
                            d2 = digits[i2]
                            for o2 in opers:
                                for i3 in range(n):
                                    if i3 != i2 and i3 != i1:
                                        d3 = digits[i3]
                                        for o3 in opers:
                                            for i4 in range(n):
                                                if i4 != i3 and i4 != i2 and i4 != i1:
                                                    d4 = digits[i4]
                                                    enum_nums = (d1, d2, d3, d4)
                                                    enum_opers = (o1, o2, o3)
                                                    if operate(enum_nums, enum_opers) == 24:
                                                        return output(enum_nums, enum_opers)
            return None
    
        result = check_24(digits, opers)
        if result:
            print(result)
        else:
            print('NONE')
  • 相关阅读:
    卸载office密钥的命令
    断言的使用
    stm32的NVIC是什么?
    STM32 Cube mx 安装
    不用移位计算获得高位数据
    分组数据
    Vue Router
    存储过程
    js 中 json.stringfy()将对象、数组转换成字符串
    js中 json对象的转化 JSON.parse()
  • 原文地址:https://www.cnblogs.com/justLittleStar/p/15002100.html
Copyright © 2011-2022 走看看