【问题标题】:Collatz Conjecture Method - JavaCollat​​z 猜想方法 - Java
【发布时间】:2016-08-13 01:48:51
【问题描述】:

我只是在学习使用 Java 中的方法。我正在尝试使用一种方法来输出使用 collat​​z 猜想达到 1 所需的步数。谁能帮助我更好地理解如何执行该方法?这是我目前所拥有的:

public static void main(String[] args) {
  collatz();     
}

public static void collatz(int n) {
  n = 20;
  int i = 0;
  if (n == 1) {         
  } else if (n % 2 == 0) {
      n = (n / 2);

  } else {
     n = (3 * n + 1);

  }
  i++;
System.out.println(i);  
}

【问题讨论】:

  • 什么不起作用?运行代码时会发生什么?请访问help center 并阅读How to Ask 以了解如何使用本网站。
  • 这不会编译,因为 collat​​z();没有被传递一个值。你应该做 collat​​z(20);

标签: java methods collatz


【解决方案1】:

这行不通,因为“i”只会在您的代码末尾被更改,并且您没有在代码中使用递归或任何类型的循环。因此,即使它确实编译了,它也不会给出正确的答案。

这是我为你做的递归方式。

public class Cycle {
    static int cycle2 (int num) {
        if (num == 1) {
            return 0;
        } else {
            if (num % 2 > 0) {
                return 1 + cycle2(num * 3 + 1);
            } else {
                return 1 + cycle2(num / 2);
            }
        }

    }
    public static void main(String[] args) {
        int num = 14;
        System.out.println(cycle2(num));
    }
}

【讨论】:

    【解决方案2】:

    据我了解,您是在询问语法(而不是算法本身),所以这是上面的另一个版本:

    public static void main(String[] args) {
      // collatz has to be called with a value or it won't compile
      collatz(20);
    }
    
    public static void collatz(int n) {
      int i = 0;
      // The following has to occur inside a loop or it'll only occur once
      while (n > 1)
      {
         // The following is what's known as "ternary form" - if the first statement is true, it'll assign the first value. Otherwise it assigns the first value.
         // For example,
         // int a = (1 == 2 ? 10 : 20);
         // will equal 20
         n = (n % 2 == 0 ?
                (n / 2) : // This value will be assigned if n is even
                (3 * n + 1)); // This value will be assigned if n is odd
         i++;
       }
      System.out.println(i);  
    }
    

    【讨论】:

      【解决方案3】:

      我知道很久以前有人问过这个问题,我也遇到过类似的问题,所以这是我的解决方案:

      public class Collatz {
         public static void main(String[] args) {
             collatz(); 
           }
      
        /*If you have (int n) inside method then 
        when you are calling collatz() you need to have
        value inside parentheses-collatz(20), or do simply like I did. 
        Also you need while loop! It will loop n (20) untill finaly get 1. 
        Otherwise your code will execute only once 
        and you will have as a result 1 step to complete instead of 7*/
      
         private static void collatz() {  
             int n = 20;                                             
             int i = 0;
             while (n != 1) {
      
               if (n % 2 == 0) {
                 n = (n / 2);
               } else {
               n = (3 * n + 1);
               }
              i++;
      
             }
          System.out.println(i);
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-09
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多