zoukankan      html  css  js  c++  java
  • java实现24点游戏代码

    import java.util.Arrays;
    import java.util.Scanner;

    public class Test07 {
        public static void main(String[] args) {

            Scanner scn = new Scanner(System.in);

            int shu[] = new int[4];
            for (int i = 0; i < 4; i++) {
                shu[i] = scn.nextInt();

            }

            // int shu[] = { 5, 9, 9, 4 };

            char op[] = { '+', '-', '*', '/' };

            A : for (int i = 0; i < 4; i++) {

                for (int n1 = 0; n1 < op.length; n1++) {

                    for (int j = 0; j < 4; j++) {

                        for (int n2 = 0; n2 < op.length; n2++) {

                            for (int k = 0; k < 4; k++) {

                                for (int n3 = 0; n3 < op.length; n3++) {

                                    for (int m = 0; m < 4; m++) {

                                        // 选的这4个数不能重复
                                        
                                        if (isAllTheOrgiNum(shu[i], shu[j], shu[k], shu[m], shu)) {
                                            double temp = 0;
                                            switch (op[n1]) {
                                            case '+':
                                                temp = shu[i] + shu[j];
                                                break;
                                            case '-':
                                                temp = shu[i] - shu[j];
                                                break;
                                            case '*':
                                                temp = shu[i] * shu[j];
                                                break;
                                            case '/':
                                                temp = (double) shu[i] / shu[j];
                                                break;
                                            }

                                            switch (op[n2]) {
                                            case '+':
                                                temp = temp + shu[k];
                                                break;
                                            case '-':
                                                temp = temp - shu[k];
                                                break;
                                            case '*':
                                                temp = temp * shu[k];
                                                break;
                                            case '/':
                                                temp = (double) temp / shu[k];
                                                break;
                                            }

                                            switch (op[n3]) {
                                            case '+':
                                                temp = temp + shu[m];
                                                break;
                                            case '-':
                                                temp = temp - shu[m];
                                                break;
                                            case '*':
                                                temp = temp * shu[m];
                                                break;
                                            case '/':
                                                temp = (double) temp / shu[m];
                                                break;
                                            }

                                            if (temp == 24) {
                                                System.out.println("(" + "(" + shu[i] + " " + op[n1] + " " + shu[j] + ")"
                                                        + " " + op[n2] + " " + shu[k] + ")" + " " + op[n3] + " " + shu[m]);
                                                break A;
                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

        private static boolean isAllTheOrgiNum(int i, int j, int k, int l, int[] shu) {
            // TODO Auto-generated method stub
            int[] a = { i, j, k, l };
            Arrays.sort(a);
            Arrays.sort(shu);
            if(a[0]==shu[0] && a[1]==shu[1] &&a[2]==shu[2] && a[3]==shu[3]) {
                return true;
            }
            return false;
        }
    }

  • 相关阅读:
    zTree 优秀的jquery树插件
    The underlying provider failed on open 问题解决
    HTML 5 <input> list 属性
    C#拖曳控件加载,bll报错问题
    把VS2010的智能代码提示和注解从英文变成中文
    progressBar的使用
    C++内存读写例子
    bash 管理小脚本
    KVM虚拟机配置笔记
    Ettercap 实施中间人攻击
  • 原文地址:https://www.cnblogs.com/zzlback/p/10357514.html
Copyright © 2011-2022 走看看