斐波那契数列的递归实现

兔子在出生两个月后,就有繁殖能力,一对兔子每个月能生出一对小兔子来。假设所有兔子都不会死去,能够一直干下去,那么以后可以繁殖多少对兔子呢?

月数      1    2    3    4    5    6    7    8    9    10    11    12

兔子对数    1    1    2    3    5    8    13    21   34    55    89    144

所以我们找到了规律,从第三个月起,每个月的兔子对数都等于前两个月的兔子对数之和

那么我们用代码来表示

package com.sbtufss.test;


public class Test {
    
    
    public static void main(String[] args) {
        int result=fbnq(12);
        System.out.println(result);
    }
    
    public static int fbnq(int mouth){
        int a=1;
        int b=1;
        if(mouth==1||mouth==2){
            return 1;
        }else{
            return fbnq(mouth-1)+fbnq(mouth-2);
        }
    }
    
}

这里注释就不写了,相信大家能看懂

结果就

原文地址:https://www.cnblogs.com/pig-brother/p/7454699.html