针对此解决方案的NZEC runtime error in SPOJ
问题获取PRIME1
。
对我做错了什么见解?
解决方案在netbeans和命令提示符下运行并提供输出。
import java.util.Scanner;
public class Main2
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num = s.nextInt();
int[] m = new int[num];
int[] n = new int[num];
for(int z=0;z<num;z++)
{
m[z] = s.nextInt();
n[z] = s.nextInt();
}
for(int z = 0 ; z<num ; z++)
{
boolean[] x = new boolean[n[z]+1];
for(int i = 2 ; i<=n[z]; i++)
x[i] = true;
for(int i = 2 ; i*i<=n[z]; i++)
{
if(x[i])
for(int j=i;i*j<=n[z];j++)
x[i*j]=false;
}
for(int i = m[z] ; i <= n[z] ; i++)
{
if(x[i]==true)
System.out.println(i);
}
System.out.println();
}
}
}
请指导我实现目标的正确方法。
答案 0 :(得分:0)
您可以将代码包装好
如果您的程序抛出任何异常,请try { /* code */ } catch (Throwable trh) { return; }
。这个问题的另一个原因可能是你的程序超出了时间限制,但不知何故你得到的是NZEC而不是TLE(主要是因为你使用的是速度慢的Scanner)。我已经读过它可能是一个SPOJ错误。
答案 1 :(得分:0)
关于NZEC错误:
NZEC代表非零退出代码。在Java中,如果抛出异常,则会生成此错误。
始终尝试在Java中实现以下格式以防止NZEC错误:
public class Main
{
public static void main(String[] args) throws IOException
{
try{
//Your Solve
}catch(Exception e){
return;
}
}
}