gpt4 book ai didi

java - Java 冰雹程序

转载 作者:行者123 更新时间:2023-12-01 10:47:22 35 4
gpt4 key购买 nike

我要编写以下程序:

An interesting (yet unsolved) question in mathematics is called "hailstone numbers". This series is produced by taking an initial integer and if the number is even, dividing it by 2. If the number is odd, multiply it by 3 and add 1. This process is the repeated.

For example: An initial number of 10 produces: 10, 5, 16, 8, 4, 2, 1, 4, 2, 1... An initial value of 23 produces: 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1...

Note that both numbers eventually reach the 4, 2, 1, 4, 2, 1... cycle.

Create an application that offers the user three different ways to run this program.

  • Option 1: Print the hailstone numbers for a single entry and its length
    Example: Input> 10 10, 5, 16, 8, 4, 2, 1 Length 7
  • Option 2: Print all of the hailstone numbers from 4 to a given entry
    Example: Input> 6 4, 2, 1 Length 3 5, 16, 8, 4, 2, 1 Length 6 6, 3, 10, 5, 16, 8, 4, 2, 1 Length 9
  • Option 3: Print out the number with the maximum number of iterations need to reach the cycle and which starting number produces this maximum from 4 to the number entered.
    Example: Input> 6 Longest: 6 Length: 9

    In writing this program you must implement the following method...

  • /** 
    *
    * @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 {

    }
    }

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

    最佳答案

    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;
    }
    }

    没有所有评论和额外的东西:

    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;
    }
    }

    关于java - Java 冰雹程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092721/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com