题目描述

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

n<=39

 

题目链接:

https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

 

package com.sunshine.OFFER66_SECOND;

import org.junit.Test;

public class A7_Fibonacci {

    @Test
    public void test() {
        System.out.println(Fibonacci(0));
    }

    public int Fibonacci(int n) {
        if(0 == n){
            return 0;
        }
        int a = 0;
        int b = 1;
        int ans = 1;
        n--;
        while (n > 0) {
            ans = a + b;
            a = b;
            b = ans;
            n--;
        }
        return ans;
    }
}

 

posted on 2019-08-27 12:55  MoonBeautiful  阅读(166)  评论(0编辑  收藏  举报