2016-12-04 107 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 –