题目要求
分析
表达式求值而已,无营养题目~~
用Java简单求解即可
AC代码(Java语言描述)
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String expression = scanner.nextLine();
scanner.close();
char[] chars = expression.toCharArray();
Queue<Character> queue = new LinkedList<>();
for (char c : chars) {
if (c == '+' || c == '-') {
queue.offer(c);
}
}
String[] numArray = expression.split("[+]|[-]");
long result = Integer.parseInt(numArray[0]);
for (int i = 1; i < numArray.length; i++) {
long temp = Integer.parseInt(numArray[i]);
if (queue.poll() == '+') {
result += temp;
} else {
result -= temp;
}
}
System.out.println(result);
}
}
Python它不香吗
香香香,怎么可能不香呢~~
Life is short, you need Python!
你知道Python怎么解这个题吗?
- Python2:
print(input())
- Python3:
print(eval(input()))
没错,这就完事,香不香~~~