24点游戏Java


import java.util.*;

/**
 * @二十四点游戏
 * @author Green.Gee
 * @date 2022/11/17 14:26
 * @email [email protected]
 */
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;
    }
}

你可能感兴趣的:(算法【,Hard,Code】,java,游戏,算法)