斐波那契数列

 面试题的总结

          最近在回顾下在面试中出现的问题.

         1, 斐波那契数列,大多数是问求第几位的数十什么。

                       斐波那契的规律:1,1,2,3,5,8,13,21,34,55,89,144....

                      主要规律:当前项=前一项+前二项

                      解题方式:递归

                      代码如下:

              public int Fibonacci(int num)     

              {

                      int result=0;

                       if(num<=2)

                       {

                              return 1;

                       }

                       result=(Fibonacci(num-1)+Fibonacci(num-2));

              }    

                      递归理解:

                      当num=5的时候

                          Fibonacci(5)

                                   第一步---Fibonacci(5)=Fibonacci(5-1)+Fibonacci(5-2);

                                         第二步--Fibonacci(5)拆分为两个Fibonacci方法

                                              第三部:Fibonacci(5-1)=Fibonacci(4)= Fibonacci(4-1)  +    Fibonacci(4-2);

                                                                                                              =Fibonacci(3)+Fibonacci(2) +Fibonacci(2)

                                                                                                              =Fibonacci(3-1)+Fibonacci(2) +     Fibonacci(2)     

                                                                                                              = Fibonacci(2)+Fibonacci(2)+Fibonacci(2)

                                                                                                              =1+1+1=3;

                                                                                          Fibonacci(4)=3;

                                                           :Fibonacci(5-2)=Fibonacci(3)=Fibonacci(3-1)+Fibonacci(3-2);

                                                                                                               = FIbonacci(2)+Fibonacci(1);

                                                                                                               =1+1=2

                                                                                      Fibonacci(3)=2 

                                                所以通过不断的自身调用寻找跟的值得出Fibonacci(5) 的值就是5;


                                                          

                                              
                  

                     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值