7 斐波那契数列

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

n<=39

 

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

 

posted @ 2017-11-05 14:56  乐乐章  阅读(182)  评论(0编辑  收藏  举报