题目 :母牛每年生一只母牛,新出生的母牛成长三年后也能每年生一只母牛,假设不会死。 求N年后,母牛的数量。 第一年 母牛生1只 一共两只 第二年 一共3只 第三年 一共4只 第四年 一共6只 第二年生的也开始生牛了 第五年 一共9只 观察其规律时间 f(n)=f(n-1)+f(n-3)
package com.algorithm.practice;
import java.time.Year;
public class CountCows {
public static int countCows(int year){
if (year==1){
return 2;
}
if (year==2){
return 3;
}
if(year==3){
return 4;
}
return countCows(year-1)+countCows(year-3);
}
public static void main(String[] args){
System.out.println(countCows(5));
}
}