Fibo递归和非递归版本

#include <iostream>
#include <time.h>
// F0=0
// F1=1
// Fn=F(n-1)+F(n-2) n>=2
using namespace std;
int Fibo(int n)
{
    int FiboMinusOne = 1;
    int FiboMinusTwo = 1;
    int result = 1;
    for (int i = 3; i <= n; i++)
    {
        FiboMinusTwo = FiboMinusOne;
        FiboMinusOne = result;
        result = FiboMinusOne + FiboMinusTwo;
    }
    return result;
}

int FiboRe(int n)
{
    int f;
    if (n < 2) f = n;
    else
        f = FiboRe(n - 1) + FiboRe(n - 2);
    return f;
}
int main()
{
    //int result = Fibo(11);	// 非递归
    clock_t start= clock();
    int result2 = FiboRe(11);	// 递归
    clock_t end = clock();
    float elapse = (float)(end - start ) / CLOCKS_PER_SEC;
    cout << "elapse = " << elapse << endl;
    cout << result << " " << result2 << endl;
    return 0;
}

你可能感兴趣的:(include,float)