递归和迭代fibonacci

时间:2018-02-21 20:21:51

标签: java recursion

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

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

1 个答案:

答案 0 :(得分:0)

你可以为递归函数添加一个单独的参数,它给出了到目前为止达到的最高Fibonacci下标(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

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