我一直试图解决这个问题: https://uchicago.kattis.com/problems/uchicago.rpn
似乎我有逻辑,但是,当我提交给Kattis时,我无法通过测试用例#7,因为我得到了错误的答案"。有人在这里,能够发现错误吗?我猜这是一个讨厌的角落案件。我可能会非常错误。
我在下面发布了我的java代码供参考。
非常感谢!
import java.util.Scanner;
import java.util.Stack;
class ReversePolishNotation
{
public static String answer()
{
Scanner scanner = new Scanner(System.in);
Stack<String> stack = new Stack<String>();
StringBuilder answer = new StringBuilder();
String s = scanner.nextLine();
scanner.close();
String[] splitStrings = s.split(" ");
int len = splitStrings.length;
for (int i = 0; i < len; i++)
{
String next = splitStrings[i];
if (next.equals("true"))
{
stack.push(next);
continue;
}
else if (next.equals("false"))
{
stack.push(next);
continue;
}
try
{
int number = Integer.parseInt(next);
stack.push(String.valueOf(number));
}
catch (NumberFormatException e)// next is an operator
{
if (!(stack.size() >= 2))
{
answer.append("SYNTAX ERROR");
return answer.toString();
}
String val1 = stack.pop();
String val2 = stack.pop();
if ((val2.equals("true")) || (val2.equals("false")))
{
if (!((val1.equals("true")) || (val1.equals("false"))))
{
answer.append("TYPE ERROR");
return answer.toString();
}
else// both values are boolean
{
if (!((next.equals("and")) || (next.equals("or"))))
{
answer.append("TYPE ERROR");
return answer.toString();
}
boolean bool1 = Boolean.parseBoolean(val1);
boolean bool2 = Boolean.parseBoolean(val2);
String result = booleanHelper(bool1, bool2, next);
stack.push(result);
}
}
else if ((val1.equals("true")) || (val1.equals("false")))
{
if (!((val2.equals("true")) || (val2.equals("false"))))
{
answer.append("TYPE ERROR");
return answer.toString();
}
else// both values are boolean
{
if (!((next.equals("and")) || (next.equals("or"))))
{
answer.append("TYPE ERROR");
return answer.toString();
}
boolean bool1 = Boolean.parseBoolean(val1);
boolean bool2 = Boolean.parseBoolean(val2);
String result = booleanHelper(bool1, bool2, next);
stack.push(result);
}
}
else
{
int num1 = Integer.parseInt(val1);
int num2 = Integer.parseInt(val2);
if (next.equals("+"))
{
String result = intHelper(num1, num2, next);
stack.push(result);
}
else if (next.equals("*"))
{
String result = intHelper(num1, num2, next);
stack.push(result);
}
else if (next.equals("=="))
{
String result = intHelper(num1, num2, next);
stack.push(result);
}
else
{
answer.append("TYPE ERROR");
return answer.toString();
}
}
}
}
if ((stack.isEmpty()) || (stack.size() > 1))
{
answer.append("SYNTAX ERROR");
return answer.toString();
}
answer.append(stack.pop());
return answer.toString();
}
private static String intHelper(int num1, int num2, String next)
{
StringBuilder answer = new StringBuilder();
if (next.equals("+"))
{
answer.append(num1 + num2);
}
else if (next.equals("*"))
{
answer.append(num1 * num2);
}
else if (next.equals("=="))
{
if (num1 != num2) answer.append("false");
else
{
answer.append("true");
}
}
return answer.toString();
}
private static String booleanHelper(boolean bool1, boolean bool2, String next)
{
StringBuilder answer = new StringBuilder();
if (next.equals("and")) answer.append(bool1 && bool2);
else if (next.equals("or")) answer.append(bool1 || bool2);
return answer.toString();
}
public static void main(String[] args)
{
System.out.println(answer());
}
}
答案 0 :(得分:1)
经过进一步测试后,我认为你唯一的错误是使用Boolean.getBoolean
而不是Boolean.parseBoolean
解析布尔值“true”和“false”。这会导致true true and
(答案:错误)等输入的错误答案。
Boolean.getBoolean
将读取具有给定名称的系统属性,并尝试将其解析为布尔值,而不是实际上您的字符串“true”和“false”。