题解 | #统计每个月兔子的总数#
统计每个月兔子的总数
http://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395
import java.util.Scanner;
public class Main {
//today--->当前月份;born--->出生月份;month--->养兔时间
public static int produce(int born, int today, int month) {
if (today <= month) {
if (today - born >= 2) {
//当前月份-出生月份 >= 2 ---->产一个崽,母兔+子兔+产下的一个崽
return 1 + produce(born, today + 1, month)
+ produce(today, today, month);
} else
//当前月份-出生月份 < 2 ---->不产崽
return produce(born, today + 1, month);
}
return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int month = in.nextInt();
//总的产崽数+最开始的基数兔(即兔子初始为1个)
int sum = produce(1, 1, month) + 1;
System.out.println(sum);
}
}
}