运行时错误 (NZEC) Java SPOJ [英] runtime error (NZEC) Java SPOJ

查看:50
本文介绍了运行时错误 (NZEC) Java SPOJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个名为 Prime Generator 的 SPOJ 问题.虽然输出在我的计算机上工作,但当我尝试在 SPOJ 上运行它时它不起作用.出现以下错误消息.

I'm trying to do a SPOJ problem called Prime Generator. Although the output works on my computer it doesn't work when I try to run it on SPOJ. The following error message occurs.

错误:运行时错误 (NZEC)

Error: runtime error (NZEC)

你能帮我找出它是什么吗?

Can you help me find what it is?

import java.util.BitSet;
import java.util.Scanner;


class Prime_generator {


public static void main(String[] args) {
    Scanner input=new Scanner(System.in);

    int number_of_entries=input.nextInt();
    int [] entries=new int[number_of_entries*2];

    for(int i=0;i<number_of_entries*2;i++){
        entries[i]=input.nextInt();
    }


    BitSet bits=new BitSet(1000000002);

    bits.set(0, 1000000000);
    bits.set(0,false);
    bits.set(1,false);
    for(int i=2;i<=Math.sqrt(1000000001);i++){

        if(bits.get(i)){

            for(int j=2;j*i<=100000000;j++){

            bits.set(j*i, false);

                }
        }

    }   

    int i=0;

    int starting_index=0;
    int ending_index=0;

    int array_index=0;
    while(i<number_of_entries){

        starting_index=entries[array_index];
        ending_index=entries[array_index+1];
        array_index+=2;
        for(int k=starting_index;k<=ending_index;k++){
            if(bits.get(k)){

                System.out.println(k);
            }

        }
        System.out.println();
        i++;
    }


    System.exit(0);


}

}

推荐答案

必须注意的几点:

  • 您必须使用快速 I/O,即 BufferedReaderBufferedWriter 类代替扫描仪(看看这篇博文).
  • 在读取输入之前对素数进行预处理(否则您将多次预先计算完整的解决方案).
  • 在退出 main 方法之前不要忘记关闭输入流.这避免了资源泄漏警告.
  • You must use fast I/O, i.e., BufferedReader and BufferedWriter classes instead of Scanner (take a look at this blog post).
  • Pre-process the prime numbers before reading the input (otherwise you would pre-compute the complete solution several times).
  • Do not forget to close your input stream(s) before you exit the main method. This avoids the resource leak warning.

这篇关于运行时错误 (NZEC) Java SPOJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆