A general non-recursive method to implement Fibonacci


/ Published in: C
Save to your folder(s)

You can put it in other programming language, like [Java](http://ideone.com/xSjGRA "Fibonacci.java")

##Fibonacci问题变种
+ 青蛙跳台

一只青蛙一次可以跳1级台阶,也可以跳2级。
求该青蛙要跳上一个n级的台阶总共有多少种跳法。

+ 矩形覆盖

我们可以用2个小正方形构成的2X1小矩形去,横着或竖着无重叠地覆盖由小正方形构成的2Xn大矩形。
请问用8个2X1小矩形去无重叠覆盖1个2X8的大矩形,总共有多少种方法。


Copy this code and paste it in your HTML
  1. int fibonacci_I (int i)
  2. {
  3. if (i < 1)
  4. {
  5. return 0;
  6. }
  7. int mth_result = 1;
  8. int var1 = 1;
  9. for (;i > 2; i--)
  10. {
  11. mth_result = mth_result + var1;
  12. var1 = mth_result - var1;
  13. }
  14. return mth_result;
  15. }

URL: http://ideone.com/xSjGRA

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.