7、斐波那契数列

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

class Solution {
public:
    int Fibonacci(int n) {
        int f0 = 0;
        int f1 = 1;
        if(n==0)
            return 0;
        if(n==1)
            return 1;
        int f;
        for(int i=2;i<=n;i++)
        {
            f = f0 + f1;
            f0 = f1;
            f1 = f;
        }
        return f;
    }
};

你可能感兴趣的:(7、斐波那契数列)