码迷,mamicode.com
首页 > 其他好文 > 详细

华为机试—24点游戏

时间:2015-06-27 16:28:21      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:24点游戏   华为机试   穷举24点   

技术分享

import java.util.Scanner;

public class dian24 {

    public boolean isresult(String s) {
        int tmp = 0;// 判断每位的值
        int pre = 0;// 0+ 1- 2* 3/
        int num = 0;// 存放计算结果
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ‘1‘ && s.charAt(i + 1) == ‘0‘) {
                tmp = 10;
                i++;
            } else if (s.charAt(i) >= ‘1‘ && s.charAt(i) <= ‘9‘) {
                tmp = s.charAt(i) - ‘0‘;
            } else {
                if (pre == 0)
                    num = num + tmp;
                else if (pre == 1)
                    num = num - tmp;
                else if (pre == 2)
                    num = num * tmp;
                else if (pre == 3)
                    num = num / tmp;
                tmp = 0;
                if (s.charAt(i) == ‘+‘)
                    pre = 0;
                else if (s.charAt(i) == ‘-‘)
                    pre = 1;
                else if (s.charAt(i) == ‘*‘)
                    pre = 2;
                else if (s.charAt(i) == ‘/‘)
                    pre = 3;
            }
        }
        if (pre == 0) {
            num = num + tmp;
        } else if (pre == 1) {
            num = num - tmp;
        } else if (pre == 2) {
            num = num * tmp;
        } else if (pre == 3) {
            num = num / tmp;
        }
        if (num == 24) {
            System.out.println(s);
            return true;
        } else
            return false;
    }

    public static void main(String[] args) {

        dian24 dian = new dian24();
        Scanner s = new Scanner(System.in);

        int count = 0;
        int ans = 0;
        int[] num = new int[4];
        for (int i = 0; i < num.length; i++) {
            num[count++] = s.nextInt();
        }

        String[] symbol = { "+", "-", "*", "/" };

        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                for (int c = 0; c < 4; c++) {
                    StringBuilder sb = new StringBuilder();
                    sb.insert(0, num[0]);
                    sb.insert(sb.length(), symbol[a]);
                    sb.insert(sb.length(), num[1]);
                    sb.insert(sb.length(), symbol[b]);
                    sb.insert(sb.length(), num[2]);
                    sb.insert(sb.length(), symbol[c]);
                    sb.insert(sb.length(), num[3]);

                    if (dian.isresult(sb.toString())) {
                        ans++;
                    }
                }
            }
        }

        if (ans > 0) {
            System.out.println("True");
        } else {
            System.out.println("False");
        }
    }

}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

华为机试—24点游戏

标签:24点游戏   华为机试   穷举24点   

原文地址:http://blog.csdn.net/wtyvhreal/article/details/46661627

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!