在 C++ 中使用递归打印斐波那契数列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21781247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:48:53  来源:igfitidea点击:

printing fibonacci series using recursion in c++

c++recursionfibonacci

提问by user3858

I just started learning c++ by myself. I am trying to practice recusion now. I want to print all n(input by user) fibonacci numbers using recursion, but it does not work. Could you help me? Thank you!!

我刚开始自学c++。我现在正在努力练习回避。我想使用递归打印所有 n(用户输入)斐波那契数,但它不起作用。你可以帮帮我吗?谢谢!!

#include <iostream>
using namespace std;
int fibonacci(int n)
{   
    if (n==1)
    {
        return 1;
        cout<<1<<" ";
    }   
    else if (n==2)
    {
        return 1;
        cout<<1<<" ";
    }
    else
    {
        return (fibonacci(n-1)+fibonacci(n-2));
        cout<<fibonacci(n-1)+fibonacci(n-2)<<" ";
    }
}
int main()
{
    int n;
    cin>>n;
    fibonacci(n);
    return 0;
}

回答by Gustav Bertram

If you look at Rosetta Code page for Fibonacci, you see that F(0) == 0and F(1) == 1.

如果您查看斐波那契的 Rosetta 代码页,您会看到F(0) == 0F(1) == 1

int fibonacci(int n)
{   

    if (n == 0)
    {
        return 0;
    }   
    else if (n == 1)
    {
        return 1;
    }
    else
    {
        return fibonacci(n-1) + fibonacci(n-2);
    }

    return fib;
}

In this case, you have a function that will calculate the fibonacci number at a specific position, right?

在这种情况下,您有一个函数可以计算特定位置的斐波那契数,对吗?

So now you need to calculate them and then print them:

所以现在你需要计算它们然后打印它们:

int main()
{
    int n;
    cin >> n;

    if (n < 0)
    {
        return -1; // This means there was an error
    }

    for (int i = 1; i < n; ++i)
    {
        cout << fibonacci(i) << " ";
    }

    return 0;
}

Note that this is not the most efficient way to do it at all, but it kinda helps you understand how recursion works.

请注意,这根本不是最有效的方法,但它可以帮助您了解递归的工作原理。

回答by Shaan

No need for cout in Fibonacci function, only cout by loop in int main()... also change IF condition in Fibonacci function to <=0 otherwise it will give you a segmentation fault (error)

Fibonacci 函数中不需要 cout,只需要在 int main() 中循环 cout ......也将 Fibonacci 函数中的 IF 条件更改为 <=0 否则它会给你一个分段错误(错误)

#include <iostream>
using namespace std;
int fibonacci(int n)
{   
    if (n<=0)
    {
        return 0;
    //    cout<<1<<" ";
    }   
    else if (n==1)
    {
        return 1;
    //    cout<<1<<" ";
    }
    else
    {
        return (fibonacci(n-1)+fibonacci(n-2));
    //   cout<<fibonacci(n-1)+fibonacci(n-2)<<" ";
    }
}
int main()
{
    int n;
    cin>>n;
    for (int x = 0; x < n; x++)
    cout << fibonacci(x) << " ";
    cout << endl;

  return 0;
}

回答by MatthieuW

Even if you place coutbefore your return statement, your code will not give you the fibonacci series in the right order. Let's say you ask for fibonacci(7). It will print all the intermediate computations for fibonacci(6), and then it will print all 1's for fibonacci(5).

即使您放在coutreturn 语句之前,您的代码也不会以正确的顺序为您提供斐波那契数列。假设您要求fibonacci(7). 它将打印 的所有中间计算fibonacci(6),然后打印所有 1 的fibonacci(5)