• 扑克牌四则运算结果为24


    计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:

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

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

    详细说明:

    1. 运算只考虑加减乘除运算,没有阶乘等特殊运算符号, 友情提醒,整数除法要当心 ;
    2. 牌面 2~10 对应的权值为 2~10, J 、 Q 、 K 、 A 权值分别为为 11 、 12 、 13 、 1 ;
    3. 输入 4 张牌为字符串形式,以 一个空格 隔开,首尾无空格;如果输入的 4 张牌中包含大小王,则输出字符串“ ERROR ”,表示无法运算;
    4. 输出的算式格式为 4 张牌通过 +-*/ 四个运算符相连, 中间无空格 , 4 张牌出现顺序任意,只要结果正确;
    5. 输出算式的运算顺序从左至右,不包含括号 ,如 1+2+3*4 的结果为 24
    6. 如果存在多种算式都能计算得出 24 ,只需输出一种即可,如果无法得出 24 ,则输出“ NONE ”表示无解。

    输入描述:

    输入4张牌为字符串形式,以一个空格隔开,首尾无空格;

    输出描述:

    如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算;

    输入

    4 2 K A

    输出

    K-A*4/2

    import sys
    nums = []
    while 1:
        line = sys.stdin.readline().strip()
        if line:
            nums.append(line)
        else:
            break
            
    def get_all_permutation(ol, lg, ret, last=-1):
        if -last == len(ol):
            ret.append(ol)
        for i in range(lg):
            l = ol[:]
            l[i], l[last] = l[last], l[i]
            get_all_permutation(l, lg=lg-1, ret=ret, last=last-1)
        return ret
    
    def get_all_ops():
        base_ops = ["+", "-", "*", "/"]
        three_ops = []
        for i in range(len(base_ops)):
            ops = base_ops[:]
            ops.pop(i)
            three_ops.append(ops)
    
        for i in range(len(base_ops)):
            ops = base_ops[:]
            same_op = ops.pop(i)
            for j in range(len(ops)):
                three_ops.append([same_op, same_op, ops[j]])
    
        # for i in range(len(base_ops)):
        #     three_ops.append([base_ops[i], base_ops[i], base_ops[i]])
        return three_ops
            
    def is_24_point(ops, nums):
        # print(ops, nums)
        op = ops[0]
        last_ret = ""
        puke_num = {1: "A", 11: "J", 12: "Q", 13: "K"}
        if op == "+":
            last_ret = nums[0] + nums[1]
        elif op == "-":
            last_ret = nums[0] - nums[1]
        elif op == "*":
            last_ret = nums[0] * nums[1]
        elif op == "/":
            if nums[0] % nums[1] == 0:
                last_ret = nums[0] / nums[1]
            else:
                return "ERROR" 
        nums_0 = puke_num.get(nums[0]) if puke_num.get(nums[0]) else nums[0]
        nums_1 = puke_num.get(nums[1]) if puke_num.get(nums[1]) else nums[1]
        last_str = str(nums_0) + op + str(nums_1)
    
        for i in range(1, len(ops)):
            if ops[i] == "+":
                last_ret = last_ret + nums[i + 1]
            elif ops[i] == "-":
                last_ret = last_ret - nums[i + 1]
            elif ops[i] == "*":
                last_ret = last_ret * nums[i + 1]
            elif ops[i] == "/":
                if last_ret % nums[i+1] == 0:
                    last_ret = last_ret / nums[i + 1]
                else:
                    return "ERROR" 
            puke_mark = puke_num.get(nums[i + 1]) if puke_num.get(nums[i + 1]) else nums[i + 1]
            last_str += ops[i] + str(puke_mark)
        # print(last_str, last_ret)
        if last_ret == 24:
            # print(ops, nums)
            # print("Correct:  ", last_str, last_ret)
            return last_str
        else:
            return "ERROR"
    
    
    for s in nums:
        arrs = s.split()
        for i, v in enumerate(arrs):
            if not str.isdigit(v):
                if v.lower() == "j":
                    arrs[i] = 11
                elif v.lower() == "q":
                    arrs[i] = 12
                elif v.lower() == "k":
                    arrs[i] = 13
                elif v.lower() == "a":
                    arrs[i] = 1
                else:
                    # print("ERROR")
                    break
            else:
                arrs[i] = int(v)
        if len(arrs) == 4:
            a, b, c, d = arrs
            num_ret = get_all_permutation(arrs, lg=4, ret=[])
            three_ops = get_all_ops()
            uqi_num_ret = []
            for num in num_ret:
                if not num in uqi_num_ret:
                    uqi_num_ret.append(num)
    
            found = False
            for nums in uqi_num_ret:
                for ops in three_ops:
                    all_ops = get_all_permutation(ops, 3, [])
                    uqi_all_ops = []
                    for item in all_ops:
                        if not item in uqi_all_ops:
                            uqi_all_ops.append(item)
                    for _ops in uqi_all_ops:
                        ret = is_24_point(_ops, nums)
                        if ret != "ERROR":
                            print(ret)
                            found = True
            if not found:
                print("NONE")
                        
    
    
    
  • 相关阅读:
    docker 操作命令
    cenos 下docker安装gitlab
    jenkins实现git钩子
    这大概是晚上最简单的翻牌效果了吧
    centos配置nginx
    putty操作指南
    阿里云centos配置jenkins
    阿里云centos配置tomcat
    利用canvas生成二维码
    生成二维码
  • 原文地址:https://www.cnblogs.com/linyihai/p/10277804.html
Copyright © 2020-2023  润新知