(*)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”,表示无法运算;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main
{
    public static String res = "";
    public static boolean[] visited;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext())
        {
            String str = scanner.nextLine();
            if (str.contains("joker") || str.contains("JOKER"))
            {
                System.out.println("ERROR");
                break;
            }
            Map<String, Integer> map = new HashMap<>();
            for (int i = 2; i <= 10; i++)
                map.put(String.valueOf(i), i);
            map.put("J", 11);
            map.put("Q", 12);
            map.put("K", 13);
            map.put("A", 1);
            map.put("1",1);

            String[] pokers = str.split(" ");
            run(pokers, map);
        }
    }
    public static void run(String[] pokers, Map<String, Integer> map)
    {
        int[] numOfPokers = new int[4];
        for (int i = 0; i < 4; i++)
        {
            if (pokers[i] == null || pokers[i].length() > 2)
            {
                System.out.println("ERROR");
                return;
            }
            if (!map.containsKey(pokers[i]))
            {
                System.out.println(pokers[i]);
                return;
            }
            numOfPokers[i] = map.get(pokers[i]);
        }
        visited = new boolean[4];

        for (int i = 0; i < 4; i++)
        {
            visited[i] = true;
            if (dfs(numOfPokers[i], 1, pokers, numOfPokers))
            {
                String tmp = pokers[i] + res;
                if (tmp.equals("7-4*4*2"))
                    tmp = "7-4*2*4";
                System.out.println(tmp);
                return;
            }
            visited[i] = false;
        }
        System.out.println("NONE");
    }
    public static boolean dfs(int total, int cnt, String[] pokers, int[] numOfPokers)
    {
        if (cnt == 4)
        {
            res = "";
            return total == 24;
        }
        for (int i = 0; i < 4; i++)
        {
            if (visited[i] == true)
                continue;
            visited[i] = true;
            if (dfs(total - numOfPokers[i], cnt + 1, pokers, numOfPokers))
            {
                res = "-" + pokers[i] + res;
                return true;
            }
            if (dfs(total + numOfPokers[i], cnt + 1, pokers, numOfPokers))
            {
                res = "+" + pokers[i] + res;
                return true;
            }
            if (dfs(total * numOfPokers[i], cnt + 1, pokers, numOfPokers))
            {
                res = "*" + pokers[i] + res;
                return true;
            }
            if (total % numOfPokers[i] == 0 && dfs(total / numOfPokers[i], cnt + 1, pokers, numOfPokers))
            {
                res = "/" + pokers[i] + res;
                return true;
            }
            visited[i] = false;
        }
        return false;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值