算法题-n月后兔子数量

有一对兔子,从出生后第5个月起每个月都生一对兔子,小兔子长到第5个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

public class test3 {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int num = fun(n);
            System.out.println(num);
        }
    }

    public static int fun(int month){
        if(month < 5){
            return 1;
        }else{
            int num = 0;
            int a1 = 1;
            int a2 = 0;
            int a3 = 0;
            int a4 = 0;
            for(int i=2; i<=month; i++){
                num += a4;
                a4 = a3;
                a3 = a2;
                a2 = a1;
                a1 = num;
            }
            return num + a1 + a2 + a3 + a4;
        }
    }
}
原文地址:https://www.cnblogs.com/mxj961116/p/12379941.html