这个是兔子队列,从第3个月开始,每个月的兔子等于前两个月的兔子的和
为什么呢?
因为前个月的兔子这个月也会存在,而前前个月的兔子正好满三个月,变成大兔子,生下来的小兔子的个数就是前前个月的
兔子的个数。
总数 = 大兔子+ 新出生的小兔子
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int monthCount = scan.nextInt();
System.out.println(getTotalCount(monthCount));
}
}
public static int getTotalCount(int monthCount)
{
int count ;
if(monthCount>=4){
count = getTotalCount(monthCount-1)+getTotalCount(monthCount-2);
}else if(monthCount==3){
count = 2;
}else {
count =1;
}
return count;
}
}