(七)

题目描述:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39

解题思路:斐波拉数列为前一项加后一项,可以列举几列后找规律去求解。

 1 public class Solution {
 2     public int Fibonacci(int n) {
 3         int one = 0;
 4         int two = 1;
 5         if(n <= 0) return one;
 6         if(n == 1) return two;
 7         int result = 0;
 8         for(int i = 2;i<=n;i++){
 9             result = one + two;
10             one = two;
11             two = result;
12         }
13         return result;
14     }
15 }

 

posted @ 2018-12-08 22:02  落水河图  阅读(498)  评论(0)    收藏  举报