【问题标题】:Hailstone Program in JavaJava 中的冰雹程序
【发布时间】:2015-12-04 16:17:44
【问题描述】:

我有以下程序要写:

数学中一个有趣(但尚未解决)的问题称为“冰雹数字”。这个数列是通过取一个初始整数,如果是偶数,除以 2。如果是奇数,乘以 3 并加 1。这个过程是重复的。

例如:初始值 10 产生:10、5、16、8、4、2、1、4、2、1... 初始值 23 产生:23、70、 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1...

请注意,这两个数字最终都会到达 4、2、1、4、2、1... 循环。

创建一个应用程序,为用户提供三种不同的方式来运行该程序。

  • 选项 1:打印单个条目的冰雹编号及其长度
    示例:输入> 10 10, 5, 16, 8, 4, 2, 1 长度7
  • 选项 2:打印从 4 到给定条目的所有冰雹编号
    示例:输入> 6 4, 2, 1 长度 3 5, 16, 8, 4, 2, 1 长度 6 6, 3, 10, 5, 16, 8, 4, 2, 1 长度 9
  • 选项 3:打印出达到循环所需的最大迭代次数的数字,以及哪个起始数字产生从 4 到输入数字的最大值。
    示例 : 输入> 6 最长: 6 长度: 9

    在编写此程序时,您必须实现以下方法...

  • /** 
     * 
     * @param num  Number that a hailstone chain will be generated 
     * @param showNumbers  true if list of numbers is shown to screen 
     * @return  Count of the numbers in the num hailstone chain.
     */
    private static int hailStone(int num, boolean showNumbers) {
        // your code
    }
    

    这是我目前写的代码:

    public static void main(String[] args) {
            int a = getInt("Give a number: ");
    
            System.out.print("How would you like to run the program? Option 1 prints hailstone numbers for a single entry and its length." +
                    "Option 2 prints all the hailstone numbers from 4 to a given entry. Option 3 prints the number with the maximum number" +
                    "of iterations needed to reach the 4, 2, 1 cycle.");
            int option = console.nextInt();
    
            boolean showNumbers = (option == 1 || option == 2);
    
            hailStone(a, showNumbers);
        }
    
        public static int getInt(String prompt) {
            int input;
    
            System.out.print(prompt);
            input = console.nextInt();
    
            return input;
        }
    
        private static void hailStone (int a, boolean showNumbers) {
            if (showNumbers == true) {
                if (a % 2 == 0) {
                    for (int i = 0; i < 50; i++) {
                       for (int j = 0; j <= i; j++)
                        a /= 2;
                        System.out.print(a + " ");
                        a *= 3;
                        a += 1;
                        System.out.print(a + " ");
                    }
    
                } else {
                    for (int i = 0; i != a; i++) {
    
                    }
                }
            } else {
    
            }
        }
    

    我觉得自己碰壁了,因为我不知道如何按照老师要求我们使用的方法来实施所有这些选项。另外,我似乎连基本的冰雹链都无法打印。帮忙?

    【问题讨论】:

    • 具体来说,您需要哪些帮助?我们无法为您解决全部问题。
    • “我似乎连基本的冰雹链都无法打印”我会先尝试让它工作。

    标签: java loops if-statement for-loop methods


    【解决方案1】:

    HailStone 算法应该不难实现。如果您将其设为递归函数,实际上会容易得多,因为这更自然将其编写为迭代函数可能是导致您的问题的原因。

    这应该足以让您入门,这是一个使用递归函数的工作 HailStone 实现。一旦你的算法工作起来,你就可以很容易地实现项目的其余要求......但是我想挑战你,一旦你得到正确的特性并编写单元测试来将它转换为一个工作迭代函数测试程序。 (TDD 规定您应该在编写实际实现之前编写测试。这是一个很好的做法,但由于时间限制和认为强大的测试套件过大的感觉而经常被跳过。)

    HailStone.java

    public class HailStone {
        /* static variable to count calls to hailStone */
        public static int iterCount = 0;
    
        /* This variable is a senti */
        public static boolean isRepeating = 0;
    
        /* Simple main function */
        public static void main(String[] args) {
            // TODO:
            //   Either parse args or use a scanner to get input.
            //   Args = verbose, entryPoint
            hailStone(10, true);
        }
    
        /* Recursive hailStone implementation */
        private static void hailStone(int a, boolean showNumbers) {
            // start off by printing the numbers if showNumbers is true
            if (showNumbers) {
                System.out.printf("Iteration #%d: %d\n", ++iterCount, a);
            }
    
            // base case: a = 1 => most important part of recursion
            if (a == 1) {
                if (isRepeating) {
                    return;
                }
                isRepeating = true;
            }
    
            // check if a is odd
            // You can use modulo divison, but we'll use bitwise &
            /* Explained: [ bitwise AND... bits that are set in a AND in 1 ]
            **********************************************
                 Case 1: a is even =>
                     a = 10
                     10 in binary is 00001010
                      1 in binary is 00000001
                ------------------------------
                 10 & 1 in binary is 00000000
    
                 Case 2: a is odd =>
                     a = 10
                     11 in binary is 00001011
                      1 in binary is 00000001
                ------------------------------
                 11 & 1 in binary is 00000001
            **********************************************
                set(X) = set of all even numbers
                set(Y) = set of all odd numbers
                {
                  x is any arbitrary number in set X,
                  y is any arbitrary number in set Y
                }
                x & 1 will ALWAYS equal 0 -\
                                            >- know this. bitwise hacks rock.
                y & 1 will ALWAYS equal 1 -/
            */
            if ((a & 1) == 1) {
                a *= 3;
                a += 1;
            } else {
                a /= 2;
            }
    
            // Tail recursion.
            hailStone(a, showNumbers);
            return;
        }
    }
    

    没有所有的 cmets 和额外的东西:

    public class HailStone {
        public static int iter_count = 0;
        public static void main(String[] args) {
            hailStone(10, true);
        }
        /* Recursive hailStone implementation */
        private static void hailStone(int a, boolean showNumbers) {
            if (showNumbers) {
                System.out.printf("Iteration #%d: %d\n", ++iter_count, a);
            }
            // base case: a = 1
            if (a == 1) {
                return;
            }
            if ((a & 1) == 1) { // a is odd:
                a *= 3;
                a += 1;
            } else {
                a /= 2;
            }
            hailStone(a, showNumbers);
            return;
        }
    }
    

    【讨论】:

      【解决方案2】:
      private static Scanner console = new Scanner(System.in);
      
      public static void main(String[] args) {
          System.out.println("How would you like to run the program?");
          System.out.println(" [1] - print hailstone numbers for a single entry and its length.");
          System.out.println(" [2] - print all hailstone numbers from 4 to a given entry.");
          System.out.println(" [3] - print the number with the maximum number of iterations needed to reach the 4, 2, 1 cycle.");
          int option = queryInt("Option: ", 1, 3);
          switch (option) {
              case 1: {
                  int seed = queryInt("INPUT> ", 1, Integer.MAX_VALUE);
                  hailStone(seed, true);
                  break;
              }
              case 2: {
                  int maxSeed = queryInt("INPUT> ", 4, Integer.MAX_VALUE);
                  for (int i = 4; i <= maxSeed; i++) {
                      hailStone(i, true);
                  }
                  break;
              }
              case 3: {
                  int maxSeed = queryInt("INPUT> ", 4, Integer.MAX_VALUE);
                  int longestChain = 0;
                  int longestChainLength = 0;
                  for (int i = 4; i <= maxSeed; i++) {
                      int length = hailStone(i, false);
                      if(length > longestChainLength) {
                          longestChain = i;
                          longestChainLength = length;
                      }
                  }
                  System.out.println("Longest: " + longestChain + " Length: " + longestChainLength);
                  break;
              }
          }
      }
      
      private static int queryInt(String prompt, int min, int max) {
          while (true) {
              System.out.print(prompt);
              String input = console.nextLine();
              try {
                  int result = Integer.parseInt(input);
                  if (result >= min && result <= max) {
                      return result;
                  } else {
                      System.err.print("Expected a number ");
                      if (min == Integer.MIN_VALUE) {
                          System.err.println(" less than or equal to " + max);
                      } else if (max == Integer.MAX_VALUE) {
                          System.err.println(" greater than or equal to " + min);
                      } else {
                          System.err.println(" between " + min + " and " + max);
                      }
                  }
              } catch (NumberFormatException ex) {
                  System.err.println("Not a number: " + input);
              }
          }
      }
      
      private static int hailStone(int num, boolean showNumbers) {
          int result = 1;
          for (Iterator<Integer> chain = iterateHailStone(num); num != 1; num = chain.next(), result++) {
              if (showNumbers) {
                  System.out.print(num + ", ");
              }
          }
          if (showNumbers) {
              System.out.print(num);
              System.out.println(" (length=" + result + ")");
          }
          return result;
      }
      
      private static Iterator<Integer> iterateHailStone(int seed) {
          return new Iterator<Integer>() {
              int value = seed;
      
              @Override
              public boolean hasNext() {
                  return true;
              }
      
              @Override
              public Integer next() {
                  if (value % 2 == 0) {
                      value /= 2;
                  } else {
                      value *= 3;
                      value++;
                  }
                  return value;
              }
          };
      }
      

      【讨论】:

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