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

 

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

 


1 package com.li.FiftyAlgorthm;
2
3 /**
4 * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子小兔子长到第三个月后每个月又生一对兔子 假如兔子都不死,问每个月的兔子总数为多少?
5 *
6 * 程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
7 *
8 * @author yejin
9 */
10 public class Rabbit {
11 public static final int MONTH = 15;
12
13 public static void main(String[] args) {
14 long f1 = 1L, f2 = 1L;
15 long f;
16 for (int i = 3; i < MONTH; i++) {
17 f = f2;
18 f2 = f1 + f2;
19 f1 = f;
20 System.out.println("第" + i + "个月的兔子对数:");
21 System.out.println("" + f2);
22 }
23 }
24