6

```package java.algorithm;

import java.util.List;

public class BornRabitCountTest {
static class RabitPair {
private int bornMonth = 1;

public RabitPair born() {
return bornMonth > 2 ? new RabitPair() : null;
}

public void incrBornMonth() {
this.bornMonth++;
}
}

public static void main(String[] args) {
int month = 10;
for (int m = 1; m <= month; m++) {
for (RabitPair rabitPair : rabitPairs) {
RabitPair newRabitPair = rabitPair.born();
rabitPair.incrBornMonth();
if (newRabitPair != null)
}
System.out.println(rabitPairs.size());
}
}

// 【程序1】
// 题目：古典问题：有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第四个月后每个月又生一对兔子，
// 假如兔子都不死，问每个月的兔子总数为多少？
public static void mainAns(String[] args) {
// 程序分析： 兔子的规律为数列1,1,2,3,5,8,13,21....
// 1 1 2 3 5
int month = 10;
for (int m = 1; m <= month; m++) {
System.out.println(f(m));
}
}

public static int f(int x) {
if (x == 1 || x == 2)
return 1;
else
return f(x - 1) + f(x - 2);
}

}```

1,1,2,3,5,8,13,21。。。

1 1 2 3 4 6 9 13 19 28

--- 共有 1 条评论 ---

--- 共有 1 条评论 ---

```if (newRabitPair != null) {
newRabitPair.incrBornMonth();
}```

```int month = 10;
System.out.println(rabitPairs.size());
for (int m = 2; m <= month; m++) {
for (RabitPair rabitPair : rabitPairs) {
rabitPair.incrBornMonth();
RabitPair newRabitPair = rabitPair.born();
if (newRabitPair != null) {
}
}
System.out.println(rabitPairs.size());
}```

--- 共有 1 条评论 ---

#### 引用来自“士止刀口”的评论

```for (RabitPair rabitPair : rabitPairs) {
rabitPair.incrBornMonth();
RabitPair newRabitPair = rabitPair.born();
if (newRabitPair != null)
}```