【发布时间】:2017-07-04 17:21:12
【问题描述】:
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0){
int n=sc.nextInt();
int pid = sc.nextInt();
Stack<Integer> st = new Stack<>();
st.push(pid);
for(int i=0;i<n;i++){
String s = sc.next();
if(s.charAt(0)=='P'){
st.pop();
st.push(Integer.parseInt(s.substring(2)));
}
}
int a = st.pop();
System.out.println("Player "+a);
t--;
}
}
}
我不知道为什么我在这个黑客地球问题中遇到运行时错误,请有人帮忙! 问题链接: https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/the-football-fest-6/
【问题讨论】:
-
您应该以与结构匹配的方式格式化代码。此外,不要声明不必要的
static变量。当你有一个计数变量时,使用for循环。并且不要通过不同的对象阅读System.in。始终使用Scanner。不需要额外的BufferedReader。 -
感谢您的回复,我已经更改了代码,然后我一直在使用扫描仪,这个在 eclipse 中显示正确的输出,但在 Hackerearth 上再次失败!它仍然显示运行时错误。
-
您正在处理
'P'事件,而不是'B'事件。你最后只弹出一次。您应该使用不同的输入进行测试。 -
Hacker earth java 编辑器对用户不友好,不要在其中浪费时间。Edior 应该像 eclipse 编辑器,IntelJi 编辑器一样友好。他们不知道如何实现友好的编辑器,只是困扰所有候选人
标签: data-structures java-8 stack