题目:
有一对兔子,从出生后第3个月起每个月都生一对兔子, 小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死, 问每个月的兔子总数为多少?
斐波那契数列:1 1 2 3 5 7 12 19 31。。。 递归
代码:
public class Rabbit {
public static void main(String[] args) {
int i =1;
while(true){
System.out.println("第"+i+"月;"+"总数:"+getNum(i)+"只");
i++;
}
}
public static long getNum(int month){
if(month==1){
return 1;
}else if(month==2){
return 1;
}
return getNum(month-1)+getNum(month-2);
}
}