【问题标题】:Recursive and iteration fibonacci递归和迭代斐波那契
【发布时间】:2018-08-01 13:24:22
【问题描述】:

我试图让它打印出代码的递归部分,就像它打印出迭代一样。 迭代中的斐波那契数列

1
1
2
3
5
8
13
21
34

斐波那契数列在 9 处的结尾是 34 时间:1。就像这样,但是每次我尝试循环打印时,我都会得到一些奇怪的答案,不确定要添加到方法中的内容。

    import java.util.Scanner;

    public class fibonacciRecursive {

    public static void main(String[] args) {
        /*
         * Dustin Willingham 
         * Class: CSCI 1302 Mon-Wed Cartersville 
         *Lets user enter the last number of a fibonacci sequence
         *then it takes the number and runs the fibonacci sequence in recursive and iteration method
         *It outputs the answer for both and the time
         */

        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number you want the Fibonacci sequence to go to:");
        int fibNumb = in.nextInt();

        //Print and timing for the Fibonacci sequence with the Recursive method
        System.out.println("Fibonacci sequence in recursion");
        //start of the timer
        long start = System.currentTimeMillis();
        System.out.println("The ending of the fibonacci sequence at " + fibNumb + " is " + fibonacciRec(fibNumb));
        System.out.print("Time : ");
        //print out the timer
        System.out.println(System.currentTimeMillis() - start);





        //Print and timing for the Fibonacci sequence with the Iteration method
        System.out.println("\n" + "Fibonacci sequence in iteration");
        start = System.currentTimeMillis();
        System.out.println("The ending of the fibonacci sequence at " + fibNumb + " is " + fiboacciIte(fibNumb));
        System.out.print("Time : ");
        System.out.print(System.currentTimeMillis() - start);


        in.close();
    }

    // Recursive method for the Fibonacci sequence
    public static int fibonacciRec(int fibNumb) {


        if ((fibNumb == 1) || (fibNumb == 0)) {
            return fibNumb;



         }
        int answer = fibonacciRec(fibNumb - 1) + fibonacciRec(fibNumb - 2);
        return answer;
    }

    //Iteration method for the Fibonacci sequence
    static int fiboacciIte(int fibNumb) {
        int a = 0, b = 1, c = 1;
        for (int d = 0; d < fibNumb; d++) {
            a = b;
            b = c;
            System.out.println(a);
            c = a + b;
        }
        return a;   
    }   
}

【问题讨论】:

    标签: java recursion


    【解决方案1】:

    您可以向递归函数添加一个单独的参数,它给出了迄今为止达到的最高斐波那契下标 (fibNumb)。如果当前的下标大于这个计数器,更新它并打印当前的斐波那契数。

    计数器必须通过引用传递,因为值需要在递归展开之前更新。我们可以为它创建一个自定义类:

    private static class Counter
    {
        private int value;
        public Counter() { value = 0; }
        public int getValue() { return value; }
        public void increment() { value++; }
    }
    

    将相同的实例传递给每个递归调用并在此过程中递增:

    private static int fibonacciRec_internal(int fibNumb_cur, Counter fibNumb_max) {
        int answer = (fibNumb_cur <= 1) ? fibNumb_cur :
                     fibonacciRec_internal(fibNumb_cur - 1, fibNumb_max) +
                     fibonacciRec_internal(fibNumb_cur - 2, fibNumb_max);
        if (fibNumb_max.getValue() < fibNumb_cur) {
            fibNumb_max.increment();
            System.out.println(answer);
        }
        return answer;
    }
    

    最后编写一个包装函数来隐藏这个计数器类及其使用细节:

    public static int fibonacciRec(int fibNumb)
    {
        Counter max = new Counter();
        return fibonacciRec_internal(fibNumb, max);
    }
    

    结果如预期:

    // fibonacciRec(10);
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    

    更好的是,我们可以为上述all 创建一个包装类,并为每个fibonacciRec 调用使用相同的对象,而不是创建一个新对象。这是一个非常微不足道的修改,所以我会留给你弄清楚什么和如何。

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2010-12-03
      • 2014-04-02
      • 2016-11-07
      • 2017-11-18
      • 2014-01-08
      • 2011-12-14
      相关资源
      最近更新 更多