美文网首页
7、斐波拉契数列

7、斐波拉契数列

作者: quiterr | 来源:发表于2017-09-02 21:36 被阅读0次

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

public class Solution {
    public int Fibonacci(int n) {
        int f1 = 0;
        int f2 = 1;
        if(n==0){
            return 0;
        }else if(n==1){
            return 1;
        }
        for(int i=2; i<=n; i++){
            int tmp = f2;
            f2 += f1;
            f1 = tmp;
        }
        return f2;
    }
}

相关文章

网友评论

      本文标题:7、斐波拉契数列

      本文链接:https://www.haomeiwen.com/subject/ekdhwttx.html