24点游戏Java


import java.util.*;

/**
 * @二十四点游戏
 * @author Green.Gee
 * @date 2022/11/17 14:26
 * @email green.gee.lu@gmail.com
 */
public class GameOf24 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        final int GAME_NUM = 24;
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int b = in.nextInt();
            int c = in.nextInt();
            int d = in.nextInt();
            String [] arr = new String[]{
                    a+"",b+"",c+"",d+""
            };
            permutations(arr,0,arr.length - 1);
            String[] symbor = { "+" , "-", "*" , "/"  };
            List<List<String>> allP = new ArrayList<>();// 所有符号组合
            for (String i1 : symbor){
                for (String i2 : symbor){
                    for (String i3 : symbor){
                        List<String> temp = new ArrayList<>();
                        temp.add(i1);
                        temp.add(i2);
                        temp.add(i3);
                        allP.add(temp);
                    }
                }
            }
            boolean flag = false;
            for(List<String> item : results){// 全排列函数替换
                for(List<String> cWord : allP){
                    double res = Double.parseDouble(item.get(0));/// 【/运算导致小数】
                    int index = 0;
                    for(int kk = 1; kk <= 3; kk++){
                        res =  calc(res,Double.parseDouble(item.get(kk)),cWord.get(index++));
                    }
                    if(res == GAME_NUM){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            System.out.println(flag);
        }
    }

    static List<List<String>> results = new ArrayList<>();
    // 数组全排列
    public static void permutations(String []a,int m,int n) {
        if (m == n) {
            List<String> temp = new ArrayList<>();
            for (int i = 0; i <= n; i++) {
                temp.add(a[i]);
            }
            if(!results.contains(temp)){
                results.add(temp);
            }
        } else {
            for (int i = m; i <= n; i++) {
                String temp = a[m];
                a[m] = a[i];
                a[i] = temp;

                permutations(a, m + 1, n);

                temp = a[m];
                a[m] = a[i];
                a[i] = temp;
            }
        }
    }

    private static double calc(double a, double b, String item) {
        double result = 0;
        switch (item) {
            case "+":
                result = a + b;
                break;
            case "-":
                result = a - b;
                break;
            case "*":
                result = a * b;
                break;
            case "/":
                result = a / b;
                break;
        }
        return result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值