---
title: "Java 兔子问题（斐波那契数列）扩展篇"
author: "zhenping"
date: "2014-08-25T12:23:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2014-10-27T20:13:53Z"
id: 2148489
url: "https://zhenping1991.iteye.com/blog/2148489"
---

[Java]() 兔子问题（[斐波那契数列]()）扩展篇

斐波那契数列指的是这样一个数列 0, 1, 1, 2,3, 5, 8, 13, 21, 34, 55, 89, 144, ...对于这个数列只能说将兔子生产周期第为3月，如果生成周期变成4月这个数列肯定不是这样的，或者说兔子还有死亡周期，在这里我是对兔子生产周期没有限定，只要月份大于生产周期都可以计算出第month月份到底能产生多少对兔子。

**Java兔子繁殖问题**

斐波那契数列又因数学家[列昂纳多·斐波那契](http://baike.baidu.com/view/1108082.htm)以兔子繁殖为例子而引入，故又称为“[兔子数列](http://baike.baidu.com/view/377065.htm)”。

一般而言，兔子在出生两个月后，就有繁殖能力，一对兔子每个月能生出一对小兔子来。如果所有兔子都不死，那么一年以后可以繁殖多少对兔子？

我们不妨拿新出生的一对小兔子分析一下：

第一个月小兔子没有繁殖能力，所以还是一对

两个月后，生下一对小兔对数共有两对

三个月以后，老兔子又生下一对，因为小兔子还没有繁殖能力，所以一共是三对。

－－－－－－

依次类推可以列出下表：

经过月数

0

1

2

3

4

5

6

7

8

9

10

11

12

幼仔对数

1

0

1

1

2

3

5

8

13

21

34

55

89

成兔对数

0

1

1

2

3

5

8

13

21

34

55

89

144

总体对数

1

1

2

3

5

8

13

21

34

55

89

144

233

幼仔对数=前月成兔对数

成兔对数=前月成兔对数+前月幼仔对数

总体对数=本月成兔对数+本月幼仔对数

可以看出幼仔对数、成兔对数、总体对数都构成了一个数列。这个数列有关十分明显的特点，那是：前面相邻两项之和，构成了后一项。

这个数列是[意大利](http://baike.baidu.com/view/3784.htm)中世纪数学家斐波那契在&lt;算盘全书&gt;中提出的，这个[级数](http://baike.baidu.com/view/132000.htm)的通项公式，除了具有a(n+2)=an+a(n+1）的性质外，还可以证明通项公式为：an=(1/√5)\*{\[(1+√5)/2]^n-\[(1-√5)/2]^n}（n=1,2,3.....）。

![](http://img.blog.csdn.net/20140825122120106?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl1emhwaW5n%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

![](http://img.blog.csdn.net/20140825121958296?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWl1emhwaW5n%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

JavaCode：

/*

\* System Abbrev ：

\* system Name :

\* Component No ：

\* Component Name：

\* File name ：FabonacciSequence.java

\* Author ：Peter.Qiu

\* Date ：Aug 25, 2014

\* Description : &lt;description&gt;

\*/

/* Updation record 1：

\* Updation date : Aug 25, 2014

\* Updator : Peter.Qiu

\* Trace No: &lt;Trace No&gt;

\* Updation No: &lt;Updation No&gt;

\* Updation Content: &lt;List all contents of updation and all methods updated.&gt;

\*/

package com.qiuzhping.util.interesting;

/\**

\* &lt;Description functions in a word&gt;

\* &lt;Detail description&gt;

\*

\* @author Peter.Qiu

\* @version \[Version NO, Aug 25, 2014]

\* @see \[Related classes/methods]

\* @since \[product/module version]

\*/

public class FabonacciSequence {

private static FabonacciSequence util = null;

/\** &lt;default constructor&gt;

\*/

public FabonacciSequence() {

// TODO Auto-generated constructor stub

}

/\** &lt;Description functions in a word&gt;

\* Aug 25, 2014

\* &lt;Detail description&gt;

\* @author Peter.Qiu

\* @param args \[Parameters description]

\* @return void \[Return type description]

\* @exception throws \[Exception] \[Exception description]

\* @see \[Related classes#Related methods#Related properties]

\*/

public static void main(String\[] args) {

long month = 8;

long product = 4;

long start = System.currentTimeMillis();

// System.out.println(" fabonacci \\trabbit : "+getInstance().fabonacci(month));

// System.out.println(" take times = "+(System.currentTimeMillis() - start)/1000);

// System.out.println("--------------------------------------");

// System.out.println(" fabonacci1 \\trabbit : "+getInstance().fabonacci1(month));

// System.out.println(" take times = "+(System.currentTimeMillis() - start)/1000);

// System.out.println("--------------------------------------");

// System.out.println(" fabonacci2 \\trabbit : "+getInstance().fabonacci2(month,product));

// System.out.println(" take times = "+(System.currentTimeMillis() - start)/1000);

for(long i = product; i &lt;= month; i++){

System.out.println("month = "+i+"\\tfabonacci2 \\trabbit : "+getInstance().fabonacci2(i,product));

}

}

public static FabonacciSequence getInstance() {

if (util == null) {

util = new FabonacciSequence();

}

return util;

}

/\** &lt;Description functions in a word&gt;

\*pruduct month = 3&lt;BR&gt;

\* Aug 25, 2014

\* &lt;Detail description&gt;

\* @author Peter.Qiu

\* @param month : How many months.

\* @return \[Parameters description]

\* @return long \[Return type description]

\* @exception throws \[Exception] \[Exception description]

\* @see \[Related classes#Related methods#Related properties]

\*/

public long fabonacci(long month) {

if (!(month &lt; 3)) {

for (long i = 3; i &lt;= month;) {

return fabonacci(month - 1) + fabonacci(month - 2);

}

}

return 1;

}

/\** &lt;Description functions in a word&gt;

\* pruduct month = 3&lt;BR&gt;

\* Aug 25, 2014

\* &lt;Detail description&gt;

\* @author Peter.Qiu

\* @param month :How many months.

\* @return \[Parameters description]

\* @return long \[Return type description]

\* @exception throws \[Exception] \[Exception description]

\* @see \[Related classes#Related methods#Related properties]

\*/

public long fabonacci1(long month) {

long sum = 1, lastMonth = 1, lastLastMonth = 1;

if (!(month &lt; 3)) {

for (long i = 3; i &lt;= month; i++) {

lastLastMonth = lastMonth;

lastMonth = sum;

sum = lastLastMonth + lastMonth;

}

}

return sum;

}

/\** &lt;Description functions in a word&gt;

\* Aug 25, 2014

\* &lt;Detail description&gt;

\* @author Peter.Qiu

\* @param month:How many months.

\* @param pruductMonth:The production cycle.

\* @return \[Parameters description]

\* @return long \[Return type description]

\* @exception throws \[Exception] \[Exception description]

\* @see \[Related classes#Related methods#Related properties]

\*/

public long fabonacci2(long month, long pruductMonth) {

long sum = 1;

if (!(month &lt; pruductMonth)) {

for (long i = 0,j = pruductMonth - 1; i &lt; month - (pruductMonth - 1); i++) {

sum += fabonacci2(month - j - i, pruductMonth);

}

}

return sum;

}

}

以上这些算法都没有应用到面向对象的思维方式去解决，如果生成周期变成4月这个数列肯定不是这样的，或者说兔子还有死亡周期，在这里我是对兔子生产周期没有限定，只要月份大于生产周期都可以计算出第month月份到底能产生多少对兔子。实际中可以将兔子定于为一个Rabbit对象，将其生产周期、死亡周期定义为属性，可能这个能有更好的效果。