Fibonacci数列的两种算法
一种是递归算法,临时输入n,递归计算出f(n);一种是先计算一遍,将结果储存起来,在用循环找出来。下面用C++将两种算法演示一遍。
1 /*First */
2 #include<iostream>
3 using namespace std;
4 int main()
5 {
6 int Fib1(int n);
7 cout<<"please enter the number you want to caculate :"<<endl;
8 int n;
9 cin>>n;
10 cout<<Fib1(n-1)<<endl; //注:第一个数是从序号0开始计数的,计算机都是这样,所 //以遵从计算机
11 return 0;
12 }
13 int Fib1(int n) //First kind of Fibonacci
14 {
15 if(0==n)
16 {
17 return 0;
18 }
19 if(1==n)
20 {
21 return 1;
22 }
23 else
24 {
25 return Fib1(n-1)+Fib1(n-2);
26 }
27 }
转载于:https://blog.51cto.com/herozem/1189060