Problem01 不死神兔

题目:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....

  即斐波那契数列。

 1 import java.util.*;
 2 
 3 public class Problem01 {
 4 
 5     public static void main(String[] args) {
 6         // 题目:
 7         // 有一对兔子,从出生后第3个月起每个月都生一对兔子
 8         // 小兔子长到第三个月后每个月又生一对兔子
 9         // 假如兔子都不死,问每个月的兔子对数为多少?
10         // 程序分析:兔子对数的规律为数列1,1,2,3,5,8,13,21...
11         // 即斐波那契数列
12         Scanner s = new Scanner(System.in);
13         System.out.println("请输入月数:");
14         int month = s.nextInt();
15         s.close();
16         System.out.println(countRabbit(month));
17 
18     }
19     
20     // 使用递归实现斐波那契数列
21     public static int countRabbit(int month) {
22         if (month==1||month==2) {
23             return 1;
24         }else {
25             return countRabbit(month-1)+countRabbit(month-2);
26         }
27     }
28 
29 }

输入月数为第9个月,输出:

1 请输入月数:
2 9
3 34
原文地址:https://www.cnblogs.com/nemowang1996/p/10387665.html