2016-05-19 3 views
0

Ich bekomme Laufzeitfehler beim Einreichen dieses Codes, während es auf idone.com und auf meinem lokalen Rechner läuft. Ich denke, dass ich Randbedingung irgendwo weglasse, aber nicht finden kann, wo.SPOJ Priming Generator geben Laufzeitfehler, während es auf Idone.com funktioniert

Hier ist mein Code.

package com.prime.src; 

import java.util.Scanner; 

class PrimeSieve { 

static boolean[] isPrime = new boolean[100000]; 

public static void main(String[] args) { 

    Scanner scanner = new Scanner(System.in); 

    for (int j = 2; j < isPrime.length; j++) { 
     isPrime[j] = true; 
    } 


    if(scanner.hasNextInt()){ 
     int N = Integer.parseInt(scanner.nextLine()); 

     int lowerBound = 0; 
     int upperBound = 0; 
     for (int i = 0; i < N; i++) { 
      lowerBound = scanner.nextInt(); 
      upperBound = scanner.nextInt(); 
      printPrime(lowerBound, upperBound); 
     } 
    } 
    else{ 
     System.out.println(); 
    } 

} 

public static void printPrime(long lowerBound, long upperBound){ 

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

     if (isPrime[i]) { 
      for (int j = i; i * j <= upperBound; j++) { 
       isPrime[i * j] = false; 
      } 
     } 
    } 

    boolean[] range = new boolean[(int) (upperBound + 1)]; 

    for (int i = (int) upperBound; i >= lowerBound; i--) { 
     range[i] = true; 
    } 

    for (int j = 2; j * j < upperBound; j++) { 
     if (isPrime[j]) { 
      long num1 = lowerBound/j; 
      long num2 = num1 * j; 
      long limit = num2; 
      for (long i = lowerBound; limit <= upperBound; i++) { 
       if (limit != j) { 
        range[(int) limit] = false; 
       } 
       limit = limit + j; 
      } 
     } 

    } 

    for (long i = lowerBound; i <= upperBound; i++) { 
     if (i == 1) 
      continue; 
     if (range[(int) i]) { 
      System.out.println(i); 
     } 
    } 


} 

} 
+0

Sie sollten den genauen Fehler hinzufügen, die Sie erhalten, und die Umgebung, in der es auftritt. Sonst wäre es schwer zu beantworten. – Frank

+0

@Frank Ich bekomme diesen Laufzeitfehler (NZEC) auf spoj, während es auf idone.com gut läuft – JimAnkit

Antwort

0

AS SPOJ ist nicht ganz ausführliche, wenn es Sie zu Exceptionhandling kommt wie folgt den Code in der Hauptmethode wickeln könnte mehr Informationen zu bekommen:

public static void main(String[] args) throws IOException 
    { 
     try { 
      //the original code in your main-Method goes here 
     } catch(Exception e){ 
      e.printStackTrace(); 
      return; 
     } 
    } 

Ich vermute, es ist ein eingewickelt Timeout, als Scanner ist nicht sehr schnell.

Verwandte Themen