fabonacci数列非递归
[cpp]#include <iostream>using namespace std;bool fibonacci(int n);void main(){cout<<"输入一个fibonacci数列的长度:"<<endl;int n;cin>>n;if(cin.fail()){cout<<"输入有误!"<<endl;exit(-1);}fibonacci(n);system("pause");}bool fibonacci(int n){if(n<1)return false;//0表示输入有误else if(1==n || 2==n)return 1;int f0=1,f1=1,temp;for(int i=3;i<n;++i){temp=f1;f1+=f0;f0=temp;}cout<<"fibonacci数列f(n)的值为:"<<f0+f1<<endl;return true;}
补充:软件开发 , C++ ,