import java.util.ArrayList; import java.util.Stack; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<Integer> inputs = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); String line = in.nextLine(); if(line != null && !line.isEmpty()) { int res = resolve(line.trim()); System.out.println(String.valueOf(res)); } } // write your code here public static int resolve(String expr) { String[] array = expr.split("\\s+"); Stack stack = new Stack(); int count = 0; for (int i = 0; i < array.length; i++){ if (array[i].equals("+")){ count--; int temp1 = 0; if (count >= 0){ temp1 = (int)stack.pop(); } else { return -1; } count--; int temp2 = 0; if (count >= 0){ temp2 = (int)stack.pop(); }else { return -1; } count++; stack.push(temp1+temp2); }else if (array[i].equals("*")){ count--; int temp1 = 0; if (count >= 0){ temp1 = (int)stack.pop(); } else { return -1; } count--; int temp2 = 0; if (count >= 0){ temp2 = (int)stack.pop(); }else { return -1; } count++; stack.push(temp1*temp2); }else if (array[i].equals("^")){ count--; int temp = 0; if (count >= 0){ temp = (int)stack.pop(); } else { return -1; } temp += 1; count++; stack.push(temp); }else { int temp = Integer.parseInt(array[i]); count++; if (count > 16){ return -2; }else { stack.push(temp); } } } return (int)stack.pop(); } } 我这样写ac了
点赞 评论

相关推荐

09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
10-24 00:54
已编辑
门头沟学院 Java
牛客20646354...:这连小厂都找不到就离谱,只能说可能你根本没投什么小厂。说实话现在都要11月了,没什么岗位了。其实最好是在9月找,那时候暑假工刚走,岗位多的是,现在都占满了岗位了,秋招的秋招,顶替暑假工的也基本上都顶替了。 只能多投了,简历其实都差不多,你这都不是外卖+点评去找实习了,已经比好多人优秀了。实在找不到,可以降低一些标准的,能投到自研项目的小厂说实话可能比你去中大厂能学到更多东西。因为中大厂最多给你看一点点模块功能,小厂基本上全部代码甚至几个项目的代码都能拿到。
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务