java-基础练习题1

/*
* 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
* 假如兔子都不死,问每个月的兔子总数为多少?
* 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
*/
这个是著名的斐波那契数列,记得公务员考试的时候见过这个题
 1     @Test
 2     public void test1() {
 3         int month = 8;
 4         System.out.println(rabbit(month));
 5     }
 6 
 7     private int rabbit(int month) {
 8         int month1 = 1;
 9         int month2 = 1;
10         int rabbitCount = 0;
11         if (month < 3) {
12             return 1;
13         } else {
14             for (int i = 3; i <= month; i++) {
15                 rabbitCount = month1 + month2;
16                 month1 = month2;
17                 month2 = rabbitCount;
18             }
19         }
20         return rabbitCount;
21     }
原文地址:https://www.cnblogs.com/liyiran/p/5753956.html