斐波那契数列

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。
 
 1 class Solution {
 2 public:     int Fibonacci(int n) {
 5         if (n ==0 || n ==1)
 6             return n;
 7         
 8         int fir = 0,sec = 1,result;
 9         for (int i = 2; i <= n ; i++)
10             {
11                 result = fir + sec;
12                 fir = sec;
13                 sec = result; 
14             }
15         return result;
16     }
17 };

 

posted @ 2016-01-21 16:20  小爷  阅读(156)  评论(0)    收藏  举报