斐波那契数列c ++递归_找到第N个斐波那契数| C ++

斐波那契数列c ++递归

Problem: Compute the Nth Fibonacci number

问题:计算第N 斐波那契数

You are given a number N. You have to find the Nth Fibonacci number. 0th Fibonacci number is 0 and first Fibonacci number is 1.

给您一个数字N。您必须找到第N 斐波那契数 0 斐波纳契数为0,第一个斐波那契数为1。

Sample inputs:

输入样例:

    N = 0, answer is 0
    N = 1, answer is 1
    N = 5, answer is 5

Basic Approach: Recursive approach

基本方法:递归方法

int fibonacci(int N){
    if(N == 0 || N == 1){
        return N;
    }
    int ans = fibonacci(N-1) + fibonacci(N - 2);
    return ans;
}

Now provided above is the basic recursive approach which takes O(2^N) time to compute as every call needs to call two other calls but if we already have the answers of two other calls in an array then we can directly use them.

现在,上面提供了基本的递归方法,该方法需要O(2 ^ N)的时间来计算,因为每个调用都需要调用另外两个调用,但是如果我们已经在数组中获得了另外两个调用的答案,则可以直接使用它们。

Top Down Approach:

自上而下的方法:

int fibonacciTopDown(int N, int strg[]){
    if(N == 1 || N == 0){
        return N;
    }
    if(strg[N] != 0){
        return strg[N];
    }
    int ans = fibonacciTopDown(N-1, strg) + fibonacciTopDown(N - 2, strg);
    strg[N] = ans;
    return ans;
}

Now the top down approach also uses recursion but before returning the answer to the solution we store the answer in strg array so that if we needed the answer again then we could simply use that answer directly from our array. The time complexity of the program is O(N).

现在,自上而下的方法也使用递归,但是在将答案返回到解决方案之前,我们将答案存储在strg数组中,这样,如果我们再次需要答案,则可以直接在数组中直接使用该答案。 程序的时间复杂度为O(N)

Bottom Up Approach:

自下而上的方法:

int fibonacciBottomUp(int N){
    int strg[N + 1] = {0};
    strg[0] = 0;
    strg[1] = 1;
    for(int i = 2;i<N+1;i++){
        strg[i] = strg[i-1] + strg[i-2];
    }
    return strg[N];
}

This is the Bottom Up approach with time complexity as O(N). Now we have an iterative method to the recursive problem.

这是自下而上的方法,其时间复杂度为O(N) 。 现在我们有了一个递归方法的迭代方法。

As seen above from the dp approaches we can very easily see that storage matrix or dp matrix has some meaning. For example, In the Fibonacci case, the Nth index of strg matrix stores the Nth fibonaaci number that's why we can very easily write this line strg[i] = strg[i-1] + strg[i-2].

从上面的dp方法可以看出,我们很容易看到存储矩阵或dp矩阵具有一定的意义。 例如,在斐波那契情况下,strg矩阵的 N 索引存储第N 斐波那契数,这就是为什么我们可以很容易地写这行strg [i] = strg [i-1] + strg [i-2]的原因 。

As stated above start solving dp problems with recursion first and then proceed to dp. Try assigning some meaning to the storage matrix or dp matrix and formulate the correct equation.

如上所述,首先使用递归解决dp问题,然后进行dp 。 尝试为存储矩阵或dp矩阵分配一些含义,并制定正确的方程式。

Code to find the Nth Fibonacci number using all given approaches

使用所有给定方法找到第N个斐波那契数的代码

#include <iostream>
using namespace std;

int fibonacci(int N){
	if(N == 0 || N == 1){
		return N;
	}
	int ans = fibonacci(N-1) + fibonacci(N - 2);
	return ans;
}

int fibonacciTopDown(int N, int strg[]){
	if(N == 1 || N == 0){
		return N;
	}
	if(strg[N] != 0){
		return strg[N];
	}
	int ans = fibonacciTopDown(N-1, strg) + fibonacciTopDown(N - 2, strg);
	strg[N] = ans;
	return ans;
}

int fibonacciBottomUp(int N){
	int strg[N + 1] = {0};
	strg[0] = 0;
	strg[1] = 1;
	for(int i = 2;i<N+1;i++){
		strg[i] = strg[i-1] + strg[i-2];
	}
	return strg[N];
}

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

	int strg[N + 1] = {0};
	cout << "The answer using recursive solution is " << fibonacci(N) << endl;
	cout << "The answer using Bottom up aproach is " << fibonacciBottomUp(N) << endl;
	cout << "The answer using Top down approach is " << fibonacciTopDown(N,strg) << endl;

	return 0;
}

Output

输出量

8
The answer using recursive solution is 21
The answer using Bottom up aproach is 21
The answer using Top down approach is 21


翻译自: https://www.includehelp.com/algorithms/find-the-nth-fibonacci-number-using-cpp.aspx

斐波那契数列c ++递归

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值