【leetcode】斐波那契数

//C语言
int fib(int N){
    if(N==0) return 0;
    if(N==1) return 1;
    int f0=0,f1=1,res,i;
    for(i=2; i<=N; i++){
        res=f0+f1;
        f0=f1;
        f1=res;
    }
    return res;
}

 

/*C++*/

class Solution {
public:
    int fib(int N) {
        if(N==0) return 0;
        if(N==1) return 1;
        int f0=0,f1=1,res,i;
        for(i=2; i<=N; i++){
            res=f0+f1;
            f0=f1;
            f1=res;
        }
        return res;
    }
};

 

posted @ 2020-10-18 19:45  温暖了寂寞  阅读(95)  评论(0编辑  收藏  举报