SPOJ上的NZEC错误 - http://www.spoj.com/problems/PRIME1/

时间:2014-08-24 14:15:47

标签: java bufferedreader

我一次又一次得到NZEC(非零退出代码)错误,因为这个代码在我的电脑上运行得非常好。请帮忙。我认为问题在于使用BufferedReader类的输入代码。我的代码是: -

import java.io.*;
class Prime1
{
public static void main(String args[])throws Exception
{
int count=0;
System.out.println("Enter the number of test cases");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
for(int i=1;i<=T;i++)
{
    System.out.println("Enter the value of m and n such that 1<=m<=n<=1000000000");
    BufferedReader br1= new BufferedReader(new InputStreamReader(System.in));
    int m=Integer.parseInt(br1.readLine());
    BufferedReader br2= new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(br2.readLine());
    for(int j=m;j<=n;j++)
    {
        for(int k=1;k<=j;k++)
        {
            if(j%k==0)
            count++;
        }
        if(count==2)
        System.out.println(j);
        count=0;
    }
    System.out.println();
}
}
}

1 个答案:

答案 0 :(得分:2)

不要打印这样的语句:

System.out.println("Enter the number of test cases");

自动检查解决方案,任何额外输出都将被视为错误答案。

您的程序不适用于以空格分隔的输入。尝试使用Scanner来读取输入。例如:

    Scanner in = new Scanner(System.in);
    int T = in.nextInt();
    for (int i = 1; i <= T; i++) {
        int m = in.nextInt();
        int n = in.nextInt();
    ...