runtime error (NZEC) Java SPOJ

自古美人都是妖i 提交于 2019-12-11 02:48:21

问题


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.

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);


}

}


回答1:


Some points that must be noted:

  • 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.


来源:https://stackoverflow.com/questions/22160814/runtime-error-nzec-java-spoj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!