Fibonacci(斐波那契数列)的实现

理论什么的就不介绍了,这里主要是从算法定义上实现的,当然还要另外几种类似的方法,和黄金分割点有关的,可以减少程序复杂度,或者进行矩阵运算,就请感兴趣的小伙伴自行理解吧!

#include 
using namespace std;
int main()
{
    int n;
    cout << "please choose a number of the ith Finonaci you want to see"<< endl;
    cin >> n;
    int i = 2;
    int fn_2 = 0, fn_1 = 1;
    if (n ==0)
    {   

        cout << "f0 = " << fn_2 << endl;
    }
    if (n == 1)
    {
        cout << "f1 = " << fn_1 << endl;
    }
    else
    {
        while (n > 1)
        {
            int fn = fn_2 + fn_1;
            cout << "the "  << i++ << "th Fibonacci is " << fn << endl;
            n--;
            fn_2 = fn_1;
            fn_1 = fn;
        }
    }

   return 0; 
}

乌拉拉!O(∩_∩)O哈哈~

你可能感兴趣的:(算法导论)