题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
public static void main(String[] args) {
//分析,第一个月:1对,第二个月:1对,第三个月:2对,第四个月:3对,第五:5对,第六:8对
//得出规律数列: 1,1,2,3,5,8 即 f(x-1)+f(x-2) x>2
for (int i = 1; i <= 10; i++) {
System.out.println(count(i));
}
}
public static int count(int x) {
if (x == 1 || x == 2) {
return 1;
}
return count(x - 1) + count(x - 2);
}
网友评论