csp201903-2 24-point game ----- java achieve

[Title] background

24.2 is a famous card game, the goal is to use its game 3 4 arithmetic operations so that
the operation result on the digital playing cards is 24.

Description [title]

Define each game consists of four numbers from 1-9, and 3 composed of four operators, operators will ensure four numbers separated twenty-two, brackets and other characters do not exist, the order of operations in accordance with the order of the four arithmetic operations. Wherein the symbol + represents addition, subtraction by the symbol - represents a multiplication lowercase letters x, the division symbol / represents. Division is divisible in the game, for example, 2/3 = 0, 3/2 = 1, 4/2 = 2.
The teacher gave you the solution n a game, you write a program to verify whether the results of each game is 24.

【data structure】

Data structure calculation expression in this item is mainly used in this last-out stack, we can look at this data structure reference books

[] Realization of ideas

Essence 24.2 expression is evaluated is the game process, implemented using two stacks in the process, one of the operator stack opt, the other is the operand stack num, as an expression to a string input, sequentially traversing each character in the string, if the subscript of an even number, illustrated as a number, which value is a number of character codes -'0 ', and the num stack is illustrated as if the operator is an odd number, when the operator stack opt direct stack is empty, the operator when the stack is not empty opt priority by comparing the top element method with the current priority of operator precedence, when the priority less than the current priority of the operator when the opt top element, stack current operators, when the top of the stack is greater than the current priority of the operator, two consecutive elements num pop the stack, the top element in conjunction with the symbol value of the current calculated two operands and operators will results added num stack, pop the top element finally operator opt stack, the next cycle of the above-described ------> opt priority comparison operator stack with the current top of the stack operator until the stack is empty or opt current operator Higher priority than the top element, the end of the cycle, and finally push the current operator, the expression scan is complete when followed by the above-described opt stack is not empty, the cycle are prioritized, pop two operands, an operator, Since the code above is repeated, so that a packaging method numAndOptpop the above steps, the idea of ​​which is detailed above as well as a method toSum value calculation expression, so much to say directly Code:

[Code]

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        Stack<Character> opt = new Stack<>();
        Stack<Integer> num = new Stack<>();
        int n = in.nextInt();
        for (int i=0;i<n;i++){
            String s = in.next();
            for (int j=0;j<s.length();j++){
                if(j%2==0){
                    num.push((int)s.charAt(j)-'0');
                }
                else {
                    if(opt.empty()){
                        opt.push(s.charAt(j));
                    }
                    else{
                        while(!opt.empty()&&priority(opt.peek(),s.charAt(j))>0){
                            numAndOptpop(num,opt);
                        }
                        opt.push(s.charAt(j));
                    }

                }
            }
            while(!opt.empty()){
                numAndOptpop(num,opt);
            }
            if(num.peek()==24)
                System.out.println("Yes");
            else
                System.out.println("No");
            num.pop();
        }
    }
    public static int toSum(int a,int b,char c){
        int sum = 0;
        switch (c){
            case '+':sum = b+a;break;
            case '-':sum = b-a;break;
            case 'x':sum = b*a;break;
            case '/':sum = b/a;break;
        }
        return sum;
    }
    public static int priority(Character a,Character b){
        int pro = 1;
        if(a=='x'||a=='/'){
            pro=1;
        }
        else {
            if(b=='x'||b=='/') {
                pro=-1;
            }
        }
        return pro;
    }
    public static void numAndOptpop(Stack<Integer> num,Stack<Character> opt){
        int num1 = num.peek();num.pop();
        int num2 = num.peek();num.pop();
        num.push(toSum(num1,num2,opt.peek()));
        opt.pop();
    }
}

Released four original articles · won praise 4 · Views 209

Guess you like

Origin blog.csdn.net/weixin_42183953/article/details/101167066