婚纱摄影网站首页,优化手机性能的软件,吉林百度seo公司,做交互的网站题目#xff1a;有一对兔子#xff0c;从出生后第3个月起每个月都生一对兔子#xff0c;小兔子长到第三个月后每个月又生一对兔子#xff0c;假如兔子都不死#xff0c;问每个月的兔子对数为多少#xff1f; 程序分析#xff1a;兔子的规律为数列1,1,2,3,5,8,13,21.... … 题目有一对兔子从出生后第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 (month1||month2) {
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