Hi,
I'm alway getting an NZEC Error by submitting my solution for PRIME1 in Java.
My approach is to save all testcases into an array first and then to determine the maximum value for the upper bound.
Then I create the Erathosthenes sieve from 0 to the maximum bound and at last I run through all the testcases in the original order to print out all the primes between the given bounds.
Here's the code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int cases;
static int [] [] eingabe;
static StringTokenizer st;
static int minBound;
static int maxBound;
static boolean [] sieve;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
cases = Integer.valueOf(input.readLine());
eingabe = new int [cases] [2];
for(int i = 0; i < cases; i++){
st = new StringTokenizer(input.readLine());
eingabe [i] [0] = Integer.valueOf(st.nextToken());
eingabe [i] [1] = Integer.valueOf(st.nextToken());
}
maxBound = getMaxBound();
createSieve();
/* Testausgabe
for(int i = 0; i < sieve.length; i++){
System.out.println("[" + i + "] = " + sieve[i]);
}
*/
for(int i = 0; i < cases; i++){
minBound = eingabe [i] [0];
maxBound = eingabe [i] [1];
for(int j = minBound; j <= maxBound; j++){
if(sieve[j])
System.out.println(j);
}
System.out.println();
}
}
static void createSieve(){
sieve = new boolean [maxBound+1];
int faktor;
for(int i = 2; i < sieve.length; i++){
sieve[i] = true;
}
for(int i = 2; i < sieve.length; i++){
if(sieve[i]){
faktor = i;
for(int j = 2; j*faktor <= maxBound; j++){
sieve[faktor*j] = false;
}
}
}
}
static int getMinBound(){
int result = eingabe [0] [0];
for(int i = 1; i < cases; i++){
if(eingabe [i] [0] < result)
result = eingabe [i] [0];
}
return result;
}
static int getMaxBound(){
int result = eingabe [0] [1];
for(int i = 1; i < cases; i++){
if(eingabe [i] [1] > result)
result = eingabe [i] [1];
}
return result;
}
}
I would be thankful for any hint. On other problems I did also get NZEC errors quite often, what is causing them in Java? The FAQ says programs crashed or uncaught exception, but that could be really much causes and the judge doesn't give me a hint on what part the error occured.
Greets,
Auron