算法题JO7:斐波那契数列

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

输出描述

示例1:
输入

输出
        
代码:

public class Solution {
    public int Fibonacci(int n) {
int fn1 = 1;
        int fn2 = 1;
        if(n <= 0 ) {
            return 0;
        }
        if(n==1 || n==2) {
            return 1;
        }
        
        while(n>2) {
            fn1 += fn2;
            fn2 = fn1-fn2;
            n--;
        }
        return fn1;

    }
}
发布了80 篇原创文章 · 获赞 1 · 访问量 1427

猜你喜欢

转载自blog.csdn.net/alidingding/article/details/104672305