2014-03-30 44 views
0

我很確定我的程序能夠正常工作,因爲它給出了與uv-output相同的結果。但它說我的回答是錯誤的。你們中的一些人可能知道這個網站。我還是初學者,沒有經驗,所以我希望能有一些幫助。也許我錯過了那個問題。 這是probelm鏈接>> (http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=36&mosmsg=Submission+received+with+ID+13417700) 這是我的代碼。UVa在線裁判-3n + 1probelm(錯誤的答案)

import java.io.*; 
import java.util.Scanner; 

class Main { 
    public static void main(String[] args) throws FileNotFoundException { 
     Main myWork=new Main(); 
     File inputFile=new File("input.txt"); 
     if(inputFile.exists()){ 
      Scanner scan=new Scanner(inputFile); 
      while(scan.hasNext()){ 
       int i=scan.nextInt(); 
       int j=scan.nextInt(); 
       int count =0; 
       for(int x=i; x<=j;++x){ 
        int y= myWork.doAlgorithem(x); 
        if(count <y) 
         count=y; 

       } 
       System.out.println(i+" "+j+" "+count); 
      }} 
     System.exit(0); 
    } 
    public static int doAlgorithem(int i) 
    { 
     int count=1; 

     while(i != 1){ 
      if(i % 2 != 0){ 
       count++; 
       i= i*3 +1; 
      } 
      else 
      { 
       count++; 
       i /=2; 
      } 
     } 
     return count; 
    } 
} 
+0

請格式化你的代碼,並添加問題陳述的描述。這個問題的鏈接也很好。 –

+1

首先當你完成main時,只返回0.不要使用System.exit(0)。 –

+4

@Naruto從'void'方法返回0嗎?無論如何,這在這種情況下不可能導致問題 –

回答

1

我相信你的解決方案的問題是,你認爲第一個整數總是小於第二個。

乳清你正在解決/比賽中的編程問題,你應該從來沒有作出任何假設。

嘗試使用這些案例的代碼,從here中提取。

1 10 
100 200 
201 210 
900 1000 
1000 900 
999999 999990 

輸出應該是:

1 10 20 
100 200 125 
201 210 89 
900 1000 174 
1000 900 174 
999999 999990 259