递归C ++函数可打印出斐波那契树的元素

时间:2018-11-25 12:32:40

标签: c++ recursion fibonacci

到目前为止,我已经能够使用递归打印出斐波那契数列。例如,序列中的前6个词(包括第0个词)是:

6

13 8 5 3 2 1 1

但是我不确定我应该如何使用它来打印出整个斐波那契树。程序输入为6时,应打印:

6
13 8 5 3 2 1 1 1 2 1 1 3 2 1 1 1 5 3 2 1 1 1 2 1 1

到目前为止,我的代码是:

#include <iostream>
#include <math.h>
#include <vector>
#include <string>

using namespace std;

std::vector<int> list;

int fib(int num);

int main() {
    int input, depth = 0, leafs = 0, size = 0;

    cin >> input;
    int temp = input;

    while (temp >= 0) {
        cout << fib(temp) << " ";
        temp--;
    }


    return 0;
}

int fib(int n) {
    if (n <= 1) {
        return 1;
    } else {
        return fib(n-1) + fib(n-2);
    }
}

1 个答案:

答案 0 :(得分:1)

如果要打印所有内容,还需要使用所有功能来打印所有内容:

protected DbCommand CurrentCommand
{
    get { return _batchCommand; }
}