码迷,mamicode.com
首页 > 其他好文 > 详细

斐波那契数列

时间:2018-10-12 14:00:55      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:style   The   sof   color   font   现在   ++   span   soft   

题目描述

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

n<=39

public class Solution {

    public int Fibonacci(int n) {

        int preNum = 1;

        int prePreNum = 0;

        int result = 0;

        if (n == 0)

            return 0;

        if (n == 1)

            return 1;

        for (int i = 2; i <= n; i++) {

            result = preNum + prePreNum;

            prePreNum = preNum;

            preNum = result;            

        }

        return result;

    }

}

斐波那契数列

标签:style   The   sof   color   font   现在   ++   span   soft   

原文地址:https://www.cnblogs.com/MarkLeeBYR/p/9777226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!