作业帮 > 综合 > 作业

用java写出兔子的规律为数列1,1,2,3,5,8,13,21.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/23 18:47:27
用java写出兔子的规律为数列1,1,2,3,5,8,13,21.
:有一对兔子,从出生后第3个月起每个月
都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,
假如兔子都不死,问每个月的兔子总数为多少?
public class Fabaccic {
/**
* 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 对兔子,假如兔子都不死,问每个月的兔子总数为多少?
*/
public static void main(String[] args) {
int month = 1;
int curr = 1;
System.out.println("month number" );
while (month < 10) {
curr = fabonaccic(month);
System.out.println(month + " " + curr);
month++;
}
}
// 在Fibonacci数列中,F[1]=1,F[2]=1,F[n]=F[n-1]+F[n-2](n>=2).
static int fabonaccic(int now) {
int newRabbit = 1; //小于2个月仍为原始个数
if (now > 2)
newRabbit = fabonaccic(now - 1) + fabonaccic(now - 2);
return newRabbit;
}
}