【问题标题】:NZEC runtime error on SPOJSPOJ 上的 NZEC 运行时错误
【发布时间】:2014-08-09 05:17:08
【问题描述】:

我的代码在 ideone 上运行成功,但在 SPOJ 上显示 NZEC 运行时错误。有人可以解释一下我的代码有什么问题吗?

import java.util.*;
class tuna{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        String[] str = new String[100];
        String pri = "+-*/^";

        Stack<String> stack = new Stack<String>();
        for(int i = 0; i<N; i++){
           str[i]=in.nextLine();
        }

    for(int i = 0; i<N; i++){
        for (int j = 0; j<str[i].length(); j++){
            char ch = str[i].charAt(j);
            if(Character.isLetter(ch))
                System.out.print(ch);
            else if(ch == '('){
                stack.push("(");
            }
            else if(ch == ')' )
            {
                while(!stack.isEmpty() && stack.lastElement()!="("){
                    System.out.print(stack.pop());

                }
                stack.pop();
            }
            else
            {
                while(!stack.empty() && stack.lastElement()!="(" && pri.indexOf(ch)<= pri.indexOf(stack.lastElement()))
                {
                    System.out.print(stack.pop());
                }
                stack.push(Character.toString(ch));
            }
        }
        while(!stack.isEmpty())
        {

            stack.pop();
        }
        System.out.println();
    }
    in.close();

  }
}

希望能提供一些关于 NZEC 错误的见解。谢谢你。 测试用例(在 spoj 上给出):

Input:
3
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))

Output:
abc*+
ab+zx+*
at+bac++cd+^*

【问题讨论】:

  • 请提出问题/测试用例
  • 您在第一个循环中要求nextLine() 多少次?为什么这会冻结在自动化测试仪上?它必须输入它指定要输入的任何数据,输入它,并且直观地知道何时输入它。

标签: java runtime-error postfix-notation


【解决方案1】:

从您的代码中查看。错误是使用 in.nextLine() 。检查Using scanner.nextLine()

更正:更改为in.next()

 for(int i = 0; i<N; i++) {
     str[i]=in.next();
 }

我真的建议您使用 BufferedReader 而不是 Scanner 来解决 SPOJ 问题以获得更好的性能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多