【项目:求Fibonacci数列】

  Fibonacci数列在计算科学、经济学等领域中广泛使用,其特点是:第一、二个数是1,从第3个数開始,每一个数是其前两个数之和。据此,这个数列为:1 1 2 3 5 8 13 21 34 55 89 ……。请设计程序,输出这个数列,直到这个数字超过10000。
  【提示】数列能够表示为:

{f1=f2=1fn=fn1+fn2,n>2

【參考解答】

#include <iostream>
using namespace std;
int main( )
{
    int f1,f2,fn,n;
    f1=f2=1;
    n=2;
    cout<<f1<<'\t'<<f2<<'\t';
    fn=f1+f2;
    while(fn<10000)
    {
        cout<<fn<<'\t';
        n++;
        if(n%5==0)
            cout<<endl;
        f1=f2;
        f2=fn;
        fn=f1+f2;
    }
    return 0;
}
posted on 2017-08-13 19:40  yutingliuyl  阅读(284)  评论(0编辑  收藏  举报