zoukankan      html  css  js  c++  java
  • CSP认证201903-2-二十四点-(Java)100分

    二十四点

    问题描述
    试题编号: 201903-2
    试题名称: 二十四点
    时间限制: 1.0s
    内存限制: 512.0MB
    在这里插入图片描述
    Java满分代码
    在这里插入图片描述
    Java代码

    import  java.util.Scanner;
    
    public class Main {
        public static int cul(int num1, Character type, int num2){
            switch (type){
                case 'x':
                    return num1*num2;
                case '/':
                    return num1/num2;
                case '+':
                    return num1+num2;
                default:
                    return num1-num2;
            }
        }
    
        public static void main(String [] args){
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            String[] output = new String[n];
            for(int i = 0;i < n;i++){
                String str = sc.next();
                int[] num = {Integer.valueOf(str.charAt(0))-48, Integer.valueOf(str.charAt(2))-48,
                        Integer.valueOf(str.charAt(4))-48, Integer.valueOf(str.charAt(6))-48};
    
                int[] oper = {0, 0, 0};     // 运算符优先级 0代表+或-, 1代表x或/
                int index = 0;
                for(int j = 1;j <= 5;j += 2, index++)
                    if(str.charAt(j) == 'x' || str.charAt(j) == '/')
                        oper[index] = 1;
                int oper_res = 0;
                if(oper[0] == 1) oper_res += 4;
                if(oper[1] == 1) oper_res += 2;
                if(oper[2] == 1) oper_res += 1;
    
                int res = 0;
                switch (oper_res){
                    case 1:     // 两边运算符优先 0 0 1
                    case 5:     //             1 0 1
                        res = cul(cul(num[0], str.charAt(1), num[1]), str.charAt(3), cul(num[2], str.charAt(5), num[3]));
                        break;
                    case 2:     // 中间运算符优先 0 1 0
                    case 3:     // 后边运算符优先 0 1 1
                        int mid = cul(num[1], str.charAt(3), num[2]);
                        int right = cul(mid, str.charAt(5), num[3]);
                        res = cul(num[0], str.charAt(1), right);
                        break;
                    default:    // 按顺序 0 0 0 , 1 0 0 , 1 1 0 , 1 1 1
                        res = cul(num[0], str.charAt(1), num[1]);
                        res = cul(res, str.charAt(3), num[2]);
                        res = cul(res, str.charAt(5), num[3]);
                }
    
                output[i] = (res == 24) ? "Yes" : "No";
            }
            for(int i = 0;i < n;i++)
                System.out.println(output[i]);
        }
    }
    
  • 相关阅读:
    解决“不是有效的win32应用程序”问题
    mysql 5.7 windows install
    Redis
    给 string 添加一个 GetInputStream 扩展方法
    定时任务为什么不用Timer
    怎样改动 VC6.0 4.0 2010 打印预览界面上的文字
    大数据时代的万象变化
    &lt;监听器模式&gt;在C++ 与 Java 之间实现的差异
    工厂模式之抽象工厂模式
    UIButton上字体的对齐方式
  • 原文地址:https://www.cnblogs.com/jiaohuadehulike/p/14294988.html
Copyright © 2011-2022 走看看