首页 > 其他 > 详细

[剑指offer]斐波那契数列

时间:2019-08-27 13:41:03      阅读:85      评论:0      收藏:0      [点我收藏+]

题目描述

大家都知道斐波那契数列,现在要求输入一个整数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;
    }
}

 

[剑指offer]斐波那契数列

原文:https://www.cnblogs.com/MoonBeautiful/p/11417706.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!