用递归方法求FIBONACCI级数。
111
#include <iostream> #include <cassert> using namespace std; int fibon(int n); void main() { int n; cout<<"请输入数列的下标"<<endl; cin>>n; if(n<=0) { cout<<"ERROR"<<endl; return; } cout<<"finbon(n)="<<fibon(n-1)<<endl; } //求数列的递归函数 int fibon(int n) { assert(n>=0); if(0==n||1==n) return 1; else return n*fibon(n-1); }
#include <iostream> #include <cassert> using namespace std; int fibon(int n); void main() { int n; cout<<"请输入数列的下标"<<endl; cin>>n; if(n<=0) { cout<<"ERROR"<<endl; return; } cout<<"finbon(n)="<<fibon(n-1)<<endl; } //求数列的递归函数 int fibon(int n) { assert(n>=0); if(0==n||1==n) return 1; else return fibon(n-1)+fibon(n-2); }