题目描述:
有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第三个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少?
/ **
*统计出兔子总数。
*
* @param month第几个月
* @return兔子总数
* /
public static int count(int month)
{
return 0;
}
输入描述:
输入int型表示一个月
输出描述:
输出兔子总数int型
示例1
输入:
9
输出:
34
分析:
这其实就是斐波那契切数列,兔子的第一个月和第二个月都是一只,当第三个月就变成2只,第四个月为2+1只,运用递归将数量变为1+1+1+1........多个一相加。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int month = sc.nextInt();
System.out.println(count(month));
}
sc.close();
}
public static int count(int month){
if(month==1||month==2){
return 1;
}
if(month == 0 ){
return 0;
}
else{
return count(month-1)+count(month-2);
}
}
}