2016-12-04 94 views
-1

您好,我正在嘗試執行hailstone序列。
雹石序列基本上是:取一個給定的整數n - 如果偶數,則序列中的下一個整數是n/2,如果是奇數,則序列中的下一個整數是n * 3 + 1.
API必須遵循對於我的任務,需要使用返回數組列表的方法執行它。
我的問題是代碼只是永遠掛起來,當我在方法本身添加輸出以查看發生了什麼事情時,我發現它總是在由於某種原因給出數字10時掛起。
我希望有一些我可能在我的條件下失蹤的小東西。使用ArrayList的Java中的Hailstone序列

下面是一些示例輸出,當給定n值15時,它會一遍又一遍地輸出。

15是奇數,所以我使它3N + 1:46
46是偶數,所以我除以2:23
23爲奇數,所以我使它3N + 1:70
70是偶數,所以我劃分通過2:35
35是奇數,所以我使它3N + 1:106
106是偶數,所以我除以2:53
53是奇數,所以我使它3N + 1:160
160是即使如此我除以2:80
80即使這樣我除以2:40
40就是這樣我除以2:20
20是偶數,所以我除以2:10
15是奇數,所以我使它3N + 1:46

我的代碼

import java.util.ArrayList; 
import java.util.Scanner; 

public class HailstoneSequence { 
    public static ArrayList<Integer> getHailstoneSequence(int n){ 
     ArrayList<Integer> results; 
     results = new ArrayList<Integer>(); 
     results.add(n); 

     //while the last number is not 1 perform these actions 
     while((results.size() - 1) != 1){ 
      //for each number in the array 
     for(int i=0; i< results.get(i); i++){ 
      //test if odd or even 
      if((results.get(i)%2)==0){ 
       System.out.println(results.get(i)+" is even so I divide by 2: "+ (results.get(i)/2)); 

        results.add((results.get(i)/2)); 

        } 
       else{ 
        //odd 
        System.out.println(results.get(i)+" is odd so I make it 3n+1: "+ (3*(results.get(i))+1)); 
        results.add((3*(results.get(i))+1)); 
       } 

     } 
     } 
     return results; 
    } 

    public static void main(String[] args) { 
     int n=0; 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter the value of n "); 
     n=sc.nextInt(); 
     sc.close(); 

     //create an initialize new array list to hold results of the hailstonesequence 
     ArrayList<Integer> list; 
     list = new ArrayList<Integer>(); 

     list = getHailstoneSequence(n); 

     //for each number in the array 
     for(int i=0; i< list.get(i); i++){ 

      if ((list.get(i)!= 1)){ 
      if((list.get(i)%2)==0){ 
        System.out.println(list.get(i)+" is even so I divide by 2: "+ (list.get(i+1))); 

        } 
       else{ 
        //odd 
        System.out.println(list.get(i)+" is odd so I make it 3n+1: "+ (list.get(i+1))); 

       } 
      } 
      else{break;} 
     } 

    } 

    } 
+0

也許永遠掛起,因爲這是在算法上做一定的投入呢? –

+0

您是否瀏覽了IDE調試器中的代碼?那是開始的地方。請訪問[help]並閱讀[ask] –

+0

調用'getHailstoneSequence'後for循環的目的是什麼? –

回答

0

在你的方法for(int i=0; i< results.get(i); i++){和在主for(int i=0; i< list.get(i); i++){

這些不會遍歷列表中的每個元素,或者至少不只是一次,如果您從未添加到列表中,它最終會導致出界。

results.get(i)是10,這是列表中唯一的數字...然後,您添加5次10次,因爲10是偶數,並且循環運行了10次。然後,您可能會添加16 5 * 10倍等等,等等。

無論如何,在遍歷它們時將元素添加到列表通常是一個壞主意。您只需要一次跟蹤兩個數字,並且可以與迭代過程分開添加到列表中。


這裏有一個working sample

ArrayList<Integer> results = new ArrayList<Integer>(); 
results.add(n); 
if (n == 1) return results; 

int next; 
if (n % 2 == 0) next = n/2; 
else next = 3*n + 1; 
results.add(next); 

while (next != 1) { 
    if (next % 2 == 0) next = next/2; 
    else next = 3*next + 1; 
    results.add(next); 
} 
return results; 
+0

我幾乎在那裏,這做的工作,所以我不再陷入無限循環,但無論輸入什麼數字它總是停在10而不是1仍然:S –

+0

如果你輸入10,應該添加10,5,16 ,8,4,2,1。這是主要方法中的for循環,可能是問題 - https://ideone.com/iWzlM9 –