• 发表于 6年前
• 阅读 285
• 收藏 3
• 评论 0

1）由题意得知：在N年内，所有羊仅在偶数年生育；羊的寿命为五年；

2）将羊圈看成一个大的容器，羊圈中最终的数量=羊的出生数量-羊的死亡数量。

0                      1                          0                              1

2                      1                          0                              1

4                      2                          0                              2

6                      3                          1                              2

8                      5                          1                              4

10                    8                          2                              6

12                    13                        3                              10

14                    21                        5                              16

16                    34                        8                              26

18                    55                        13                            42

20                    89                        21                            68

/**
* 获得N年后羊总数量
* @param year 多少年
* @return
*/
public static long getTotalCountByYear(int year){
if(year%2!=0) year--;
long _1thCount = 2;// 第6年增长数量
long _2thCount = 4;// 第8年增长数量
long _3rdCount = _2thCount+_1thCount;// 第10年增长数量
long totalCount = _1thCount+_2thCount+_3rdCount;
System.out.print(_1thCount+","+_2thCount+","+_3rdCount+",");
for(int i=12;i<=year;i=i+2){
_1thCount = _2thCount;
_2thCount = _3rdCount;
_3rdCount = _2thCount+_1thCount;
totalCount = totalCount + _3rdCount;
System.out.print(_3rdCount+",");
}
return 1+1+2+totalCount;
}

×