剑指Offer——斐波那契数列

1、题目描述

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

2、代码实现

 1 package com.baozi.offer;
 2 
 3 /**
 4  * @author BaoZi
 5  * @create 2019-07-11-10:16
 6  */
 7 public class Offer7 {
 8     public static void main(String[] args) {
 9         Offer7 offer7 = new Offer7();
10         int fibonacci = offer7.Fibonacci(10);
11         System.out.println(fibonacci);
12     }
13     //斐波那契数列的特点:
14 
15     /**
16      * 第n项     0  1  2   3   4   5   6   7   8   9   10......
17      * 第n项的值 0  1  1   2   3   5   8   13  21  34  55.......
18      *
19      * @param n 整数n代表斐波那契额数列中的第n项
20      * @return 返回的就是斐波那契数列中第n项的值
21      */
22     public int Fibonacci(int n) {
23         int result = 0;
24         if (n == 0) {
25             result = 0;
26         }
27         if (n == 1) {
28             result = 1;
29         }
30         if (n >= 2) {
31             result = Fibonacci(n - 1) + Fibonacci(n - 2);
32         }
33         return result;
34     }
35 }
原文地址:https://www.cnblogs.com/BaoZiY/p/11168426.html