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

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;

}

}