表达式计算:
1、使用栈
public int clumsy(int N) {
Stack<Integer> stack = new Stack<>();
char[] op = new char[]{'*', '/', '+', '-'};
stack.push(N--);
int index = 0;
while (N > 0) {
if (op[index] == '*') {
stack.push(stack.pop() * N--);
} else if (op[index] == '/') {
stack.push(stack.pop() / N--);
} else if (op[index] == '+') {
stack.push(N--);
} else if (op[index] == '-') {
stack.push(-1 * (N--));
}
index = (index + 1) % 4;
}
int sum = 0;
while (!stack.isEmpty()) {
sum += stack.pop();
}
return sum;
}
2、充分利用题目中表达式的规律,递归计算
public int clumsy(int N) {
return clumsy(N, false);
}
int clumsy(int N, boolean inverse) {
if (N >= 4 && !inverse) return N * (N - 1) / (N - 2) + (N - 3) - clumsy(N - 4, !inverse);
else if (N >= 4 && inverse) return N * (N - 1) / (N - 2) - (N - 3) + clumsy(N - 4, inverse);
else if (N == 3) return 3 * 2 / 1;
else if (N == 2) return 2 * 1;
else if (N == 1) return 1;
return 0;
}