周欣雨同学的java编程题之兔子问题
1.题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?
分析:兔子的规律为数列1,1,2,3,5,8,13,21…
public class Tuzi {
public static void main(String[] args) {
int n=sc.nextInt();
System.out.println("第"+n+"个月兔子总数为"+method(n));
}
private static int method(int n) {
if(n==1||n==2) {
return 1;
}else {
return method(n-1)+method(n-2);
}
}
}