输入输出样例

样例1

输入

  1. 10
  2. 9+3+4x3
  3. 5+4x5x5
  4. 7-9-9+8
  5. 5x6/5x4
  6. 3+5+7+9
  7. 1x1+9-9
  8. 1x9-5/9
  9. 8/5+6x9
  10. 6x7-3x6
  11. 6x4+4/5

输出

  1. Yes
  2. No
  3. No
  4. Yes
  5. Yes
  6. No
  7. No
  8. No
  9. Yes
  10. Yes

题解一

参见中缀表达式求值。100分。

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Deque;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. public class Main {
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11. int n = Integer.parseInt(reader.readLine().trim());
  12. for (int i = 0; i < n; ++i) {
  13. int ans = cal(reader.readLine().trim());
  14. if (ans == 24) {
  15. System.out.println("Yes");
  16. } else {
  17. System.out.println("No");
  18. }
  19. }
  20. }
  21. private static int cal(String s) {
  22. return calculate(infixToPostfix(infixToList(s)));
  23. }
  24. private static List<String> infixToList(String expression) {
  25. List<String> ans = new LinkedList<String>();
  26. char[] chars = expression.toCharArray();
  27. for (int begin = 0, i = 0; i < chars.length;) {
  28. // 符号
  29. if ((chars[i] < '0') || (chars[i] > '9')) {
  30. ans.add(chars[i] + "");
  31. ++i;
  32. begin = i;
  33. } else { // 整字
  34. while ((i < chars.length) && (chars[i] >= '0') && (chars[i] <= '9')) {
  35. ++i;
  36. }
  37. ans.add(expression.substring(begin, i));
  38. begin = i;
  39. }
  40. }
  41. return ans;
  42. }
  43. private static List<String> infixToPostfix(List<String> infixList) {
  44. List<String> ans = new ArrayList<>(infixList.size());
  45. // 符号栈
  46. Deque<String> stack = new LinkedList<>();
  47. for (String e : infixList) {
  48. if (e.equals("(")) { // 左括号直接入栈
  49. stack.push(e);
  50. } else if (e.equals(")")) { // 右括号入栈时, 一直出栈直到遇到将知道一个左括号
  51. while (!stack.peek().equals("(")) {
  52. // 栈顶元素出栈入队
  53. ans.add(stack.pop());
  54. }
  55. // 左括号直接出栈
  56. stack.pop();
  57. } else if (isOperator(e)) { // 遇到操作符
  58. // 栈为空或者栈顶元素优先级低则入栈
  59. if (stack.isEmpty() || (getPriority(stack.peek()) < getPriority(e))) {
  60. stack.push(e);
  61. } else {
  62. // 栈不为空且栈顶元素不为左括号且栈顶元素优先级大于党员元素, 则持续出栈入队
  63. while ((!stack.isEmpty()) && (!stack.peek().equals("("))
  64. && (getPriority(stack.peek()) >= getPriority(e))) {
  65. ans.add(stack.pop());
  66. }
  67. stack.push(e);
  68. }
  69. } else { // 遇到数字, 直接入队
  70. ans.add(e);
  71. }
  72. }
  73. while (!stack.isEmpty()) {
  74. ans.add(stack.pop());
  75. }
  76. return ans;
  77. }
  78. private static boolean isOperator(String s) {
  79. return s.equals("+") || s.equals("-") || s.equals("x") || s.equals("/");
  80. }
  81. private static int getPriority(String s) {
  82. switch (s) {
  83. case "+":
  84. case "-":
  85. return 1;
  86. case "x":
  87. case "/":
  88. return 2;
  89. }
  90. // 左括号优先级最低
  91. return -1;
  92. }
  93. private static int calculate(List<String> list) {
  94. // 结果栈
  95. Deque<Integer> stack = new LinkedList<>();
  96. int num1, num2;
  97. for (String e : list) {
  98. if (isOperator(e)) {
  99. num2 = stack.pop();
  100. num1 = stack.pop();
  101. switch (e) {
  102. case "+":
  103. stack.push(num1 + num2);
  104. break;
  105. case "-":
  106. stack.push(num1 - num2);
  107. break;
  108. case "x":
  109. stack.push(num1 * num2);
  110. break;
  111. case "/":
  112. stack.push(num1 / num2);
  113. break;
  114. }
  115. } else {
  116. stack.push(Integer.parseInt(e));
  117. }
  118. }
  119. return stack.pop();
  120. }
  121. }