由于此解决方案的 PRIME1
问题,在 SPOJ 中出现 NZEC 运行时错误
。
关于我做错了什么有什么见解吗?
该解决方案运行并在 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();
}
}
}
请指导我实现目标的正确方法。
最佳答案
关于 NZEC 错误:
NZEC 代表非零退出代码。在 Java 中,如果抛出异常,就会生成此错误。
始终尝试在 Java 中实现以下格式以防止 NZEC 错误:
public class Main
{
public static void main(String[] args) throws IOException
{
try{
//Your Solve
}catch(Exception e){
return;
}
}
}
关于java - 在 SPOJ 中针对此代码收到 NZEC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24571469/