简体   繁体   English

斐波那契数的递归函数

[英]Recursive function for Fibonacci number

I have to write a simple program as follows: "Given a non-negative integer n, find the nth Fibonacci number using recursion".我必须编写一个简单的程序如下:“给定一个非负整数 n,使用递归找到第 n 个斐波那契数”。 I think what this means is that, for any value entered by the user, I have to get the Fibonacci number.我认为这意味着,对于用户输入的任何值,我都必须获得斐波那契数。 For example, if the user entered 4, I would have to get the 4th value in the list of Fibonacci numbers (which is 2).例如,如果用户输入 4,我将必须获得斐波那契数列中的第 4 个值(即 2)。 Below is what I have written, but there is something wrong with my recursion as it crashes when I run it.下面是我写的,但是我的递归有问题,因为它在我运行时崩溃了。 Appreciate any help...感谢任何帮助...

int userValue;
int fibo;
int fib(int n);
int fibValue;


int main() {
    cout << "Please provide your value" << endl;
    cin >> userValue;

    while (userValue < 0) {
        cout << "Fibonacci numbers only start at 0, please try again: " << endl;
        cin >> userValue;
    }

    if (userValue == 1 || userValue == 0) {
        cout << "Fibonacci result is: " << userValue << endl;
        return 0;
    }
    else {
        fib(userValue);
        cout << "Fibonacci result is: " << fibValue << endl;
        //return 0;
    }
}

int fib(int n)
{
    fibValue = fib(n - 1) + fib(n - 2);
    return fibValue;
}

The problem lies in fib method.There is no termination condition provided.问题在于fib方法。没有提供终止条件。 So, The recursion will happen in a loop without terminating.因此,递归将在循环中发生而不会终止。

First, try to debug any problem by giving multiple inputs and you will understand where the problem lies.首先,尝试通过提供多个输入来调试任何问题,您就会明白问题出在哪里。

In your case,在你的情况下,

For suppose n=3 ,对于假设n=3

the trace would be like this痕迹会是这样的

fib(3) -> which further invokes fib(2) and fib(1)

fib(2) -> which further invokes fib(1) and fib(0)

now since there is no termination condition现在因为没有终止条件

fib(0) will further invoke fib(-1) and fib(-2)

since fib of negative value does not exists termination condition should be provided so that recursion would stop and return the result.由于不存在负值的 fib 应提供终止条件,以便递归停止并返回结果。

For fibonacci number, termination condition would be like:对于斐波那契数,终止条件如下:

 if(n == 0){
  return 0;
 }else if (n == 1){
  return 1;
 }

Few reference很少参考

https://blog.hartleybrody.com/debugging-code-beginner/ https://blog.hartleybrody.com/debugging-code-beginner/

https://www.codementor.io/mattgoldspink/how-to-debug-code-efficiently-and-effectively-du107u9jh%60 https://www.codementor.io/mattgoldspink/how-to-debug-code-efficiently-and-effectively-du107u9jh%60

Hope this helps.希望这可以帮助。 Thanks.谢谢。

Your recursion's stop condition is outside of it, in the following if:您的递归的停止条件在它之外,在以下情况下:

if (userValue == 1 || userValue == 0) {...}

But it should instead be in the fib function.但它应该在 fib 函数中。

What you currently have is an endless recursion which is the shortest path to a stack overflow.您目前拥有的是无限递归,这是堆栈溢出的最短路径。

Recursion in the above code never stops, it stops only when stack is full resulting in run-time termination of program.上面代码中的递归永远不会停止,只有在堆栈已满导致程序运行时终止时才会停止。 For you program:为您计划:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM