Submission #3238670


Source Code Expand

public static  int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<tokens.length;i++) {
            try {
                int num=Integer.parseInt(tokens[i]);
                stack.add(num);
            } catch (Exception e) {
                // TODO: handle exception
                int a=stack.pop();
                int b=stack.pop();
                stack.add(calculate(a,b,tokens[i]));
            }
        }
        return stack.pop();    
    }
    
    public static  int calculate(int a, int b,String opera) {
        switch(opera) {
        case "+":return b+a;
        case "-":return b-a;
        case "*":return b*a;
        case "/":return b/a;
        default:return 0;
        }        
    }

---------------------

本文来自 BabaloveU 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/weixin_38584649/article/details/79763013?utm_source=copy 

Submission Info

Submission Time
Task F - 逆ポーランド記法
User luogu_bot4
Language Pascal (FPC 2.6.2)
Score 0
Code Size 932 Byte
Status CE