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;
        }
    }

  • 相关阅读:
    MySQL 索引优化指南
    Servlet,Tomcat,Jetty,Netty,Mina 对比
    Linux 磁盘IO速度测试
    flume 数据链路问题排查
    MySQL 强行kill后启动失败问题:[ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
    hdfs web界面介绍
    python2/python3 编码问题
    Spark WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting port 1.
    python2 升级到 python3
    万维网的信息检索系统【计算机网络】
  • 原文地址:https://www.cnblogs.com/zzlback/p/10357514.html
Copyright © 2011-2022 走看看