题解 | #累加出整个范围所有的数最少还需要几个数#
公式字符串求值
http://www.nowcoder.com/practice/c590e97ee1f6462d871430feee055d25
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main{
public static void main(String[] args) throws IOException { BufferedReader sc = new BufferedReader(new InputStreamReader(System.in)); String input = sc.readLine(); int result = process(input); System.out.println(result); } public static int process(String input) { char[] ch = input.toCharArray(); Stack<Character> operatorStack = new Stack<>(); Stack<Integer> dataStack = new Stack<>(); if (ch[0] == '-') { dataStack.push(0); } int count = 0; for (int i = 0; i < ch.length; i++) { if (ch[i] >= '0' && ch[i] <= '9') { count++; if (i + 1 < ch.length && !(ch[i + 1] >= '0' && ch[i + 1] <= '9')) { String num = input.substring(i - count + 1, i + 1); dataStack.push(Integer.parseInt(num)); count = 0; } else if (i == ch.length - 1) { String num = input.substring(i - count + 1); dataStack.push(Integer.parseInt(num)); } } else if (ch[i] == '(') { operatorStack.push(ch[i]); } else if (ch[i] == '*' || ch[i] == '/') { while (operatorStack.size() != 0 && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) { Integer passiveNum = dataStack.pop(); Integer ObjectiveNum = dataStack.pop(); Character operator = operatorStack.pop(); Integer value = -1; if (operator == '*') { value = ObjectiveNum * passiveNum; } else if (operator == '/') { value = ObjectiveNum / passiveNum; } dataStack.push(value); } operatorStack.push(ch[i]); } else if (ch[i] == '+' || ch[i] == '-') { if (i-1>=0&&ch[i]=='-'&&ch[i-1]=='('){ dataStack.push(0); } if (operatorStack.size() == 0 || (operatorStack.size() > 1 && operatorStack.peek() != '*' && operatorStack.peek() != '/' && operatorStack.peek() != '+' && operatorStack.peek() != '-')) { operatorStack.push(ch[i]); } else { while (operatorStack.size() != 0 && (operatorStack.peek() == '*' || operatorStack.peek() == '/' || operatorStack.peek() == '+' || operatorStack.peek() == '-')) { Integer passiveNum = dataStack.pop(); Integer ObjectiveNum = dataStack.pop(); Character operator = operatorStack.pop(); Integer value = -1; if (operator == '*') { value = ObjectiveNum * passiveNum; } else if (operator == '/') { value = ObjectiveNum / passiveNum; } else if (operator == '+') { value = ObjectiveNum + passiveNum; } else if (operator == '-') { value = ObjectiveNum - passiveNum; } dataStack.push(value); } operatorStack.push(ch[i]); } } else if (ch[i] == ')') { while (operatorStack.size() != 0 && operatorStack.peek() != '(') { Integer passiveNum = dataStack.pop(); Integer ObjectiveNum = dataStack.pop(); Character operator = operatorStack.pop(); Integer value = -1; if (operator == '*') { value = ObjectiveNum * passiveNum; } else if (operator == '/') { value = ObjectiveNum / passiveNum; } else if (operator == '+') { value = ObjectiveNum + passiveNum; } else if (operator == '-') { value = ObjectiveNum - passiveNum; } dataStack.push(value); } operatorStack.pop(); } } while (!operatorStack.isEmpty()) { Integer passiveNum = dataStack.pop(); Integer ObjectiveNum = dataStack.pop(); Character operator = operatorStack.pop(); Integer value = -1; if (operator == '*') { value = ObjectiveNum * passiveNum; } else if (operator == '/') { value = ObjectiveNum / passiveNum; } else if (operator == '+') { value = ObjectiveNum + passiveNum; } else if (operator == '-') { value = ObjectiveNum - passiveNum; } dataStack.push(value); } return dataStack.peek(); }
}