剑指Offer——斐波那契数列
生活随笔
收集整理的這篇文章主要介紹了
剑指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
總結
以上是生活随笔為你收集整理的剑指Offer——斐波那契数列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UIBezierPath和CAShape
- 下一篇: 聊聊springboot2的embede