2015-04-02 36 views
0

我正在解決UVA上的問題。它不斷給運行時錯誤。我已經修改了我的代碼,以便在閱讀有關stackoverflow的類似問題的答案後以正確的格式輸入,但仍然存在運行時錯誤。甚至在格式化輸入後,uva online法官中的JAVA運行時錯誤

請問我的代碼中有什麼問題? 這裏的問題鏈接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

代碼Java中:

class P100 { 

    public static void main(String[] args) throws IOException { 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

     long a[] = new long[1000000]; 
     Scanner sc ; 

     String line = " "; 
     while ((line = br.readLine()) != null) { 
      sc=new Scanner(line); 
      long l,r; 
      if(sc.hasNextLong()) 
      l = sc.nextLong(); 
      else 
       break; 
      if(sc.hasNextLong()) 
      r = sc.nextLong(); 
      else 
       break; 
      long i = l, j = r; 
      if (l > r) { 
       i = r; 
       j=l; 
      } 
      long max = Long.MIN_VALUE; 
      for (long k = i; k < r + 1; k++) { 
       if (a[(int)k] == 0) { 
        a[(int)k] = countCycleLength(k); 
       } 
       if (max < a[(int)k]) { 
        max = a[(int)k]; 
       } 
      } 
      System.out.println(l + " " + r + " " + max); 

     } 

    } 

    static long countCycleLength(long j) { 
     long count = 1; 
     while (j != 1) { 
      if (j % 2 != 0) { 
       j = j * 3 + 1; 
       j = j/2; 
       count += 2; 
      } else { 
       j = j/2; 
       count++; 
      } 

     } 

     return count; 
    } 

    static long ip(String s) { 
     return Long.parseLong(s); 
    } 

} 

回答

0

最後我得到這個代碼通過改變類的名稱從P100到主要和改變for循環爲k <條件接受J + 1。