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

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

Java兔子生殖问题

斐波那契数列又因数学家列昂纳多·斐波那契以兔子生殖为样例而引入。故又称为“兔子数列”。

一般而言,兔子在出生两个月后。就有生殖能力。一对兔子每一个月能生出一对小兔子来。假设全部兔子都不死,那么一年以后能够生殖多少对兔子?

我们最好还是拿新出生的一对小兔子分析一下:

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

两个月后,生下一对小兔对数共同拥有两对

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

------

依次类推能够列出下表:

经过月数

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

幼仔对数=前月成兔对数

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

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

能够看出幼仔对数、成兔对数、整体对数都构成了一个数列。这个数列有关十分明显的特点,那是:前面相邻两项之和,构成了后一项。

这个数列是意大利中世纪数学家斐波那契在<算盘全书>中提出的。这个级数的通项公式,除了具有a(n+2)=an+a(n+1)的性质外,还能够证明通项公式为:an=(1/√5)*{[(1+√5)/2]^n-[(1-√5)/2]^n}(n=1,2,3.....)。

JavaCode:

/*

* System Abbrev :

* system Name  :

* Component No  :

* Component Name:

* File name     :FabonacciSequence.java

* Author        :Peter.Qiu

* Date          :Aug 25, 2014

* Description   :  <description>

*/

/* Updation record 1:

* Updation date        :  Aug 25, 2014

* Updator          :  Peter.Qiu

* Trace No:  <Trace No>

* Updation No:  <Updation No>

* Updation Content:  <List all contents of updation and all methods updated.>

*/

package com.qiuzhping.util.interesting;

/**

* <Description functions in a word>

* <Detail description>

*

* @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;

/** <default constructor>

*/

public FabonacciSequence() {

// TODO Auto-generated constructor stub

}

/** <Description functions in a word>

* Aug 25, 2014

* <Detail description>

* @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 <= 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;

}

/** <Description functions in a word>

*pruduct month = 3<BR>

* Aug 25, 2014

* <Detail description>

* @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 < 3)) {

for (long i = 3; i <= month;) {

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

}

}

return 1;

}

/** <Description functions in a word>

* pruduct month = 3<BR>

* Aug 25, 2014

* <Detail description>

* @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 < 3)) {

for (long i = 3; i <= month; i++) {

lastLastMonth = lastMonth;

lastMonth = sum;

sum = lastLastMonth + lastMonth;

}

}

return sum;

}

/** <Description functions in a word>

* Aug 25, 2014

* <Detail description>

* @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 < pruductMonth)) {

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

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

}

}

return sum;

}

}

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

Java 兔子问题(斐波那契数列)扩展篇的更多相关文章

  1. Java迭代实现斐波那契数列

    剑指offer第九题Java实现 题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. public class Test9 { public static void ...

  2. Java练习 SDUT-1132_斐波那契数列

    C/C++经典程序训练2---斐波那契数列 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写计算斐波那契(Fibon ...

  3. Java学习之斐波那契数列实现

    描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...

  4. Python学习之斐波那契数列实现篇

    描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...

  5. (java版)斐波那契数列

    用JAVA编写Fibonacei(1,1,2,3,5,8,13...)数列的第n项 分析:当n=1时,a(n)=1;当n=2时 ,a(n)=2. 所以当n=>3时,a(n)=a(n-1)+a(n ...

  6. 简单Java算法程序实现!斐波那契数列函数~

    java编程基础--斐波那契数列 问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:可能出现的情况:(1) n=1 ,一种方法 ;(2)n=2 ...

  7. 斐波那契数列 的两种实现方式(Java)

    import java.util.Scanner; /* 斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 如果设F(n)为该数列的第n ...

  8. java大数 斐波那契数列

    java大数做斐波那契数列:  思路:1.       2.可以用数组存着 import java.math.BigInteger; import java.util.Scanner; public ...

  9. 《剑指offer》第十题(斐波那契数列)

    // 面试题:斐波那契数列 // 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项. #include <iostream> using namespace std; ...

  10. Java经典案例之-判断兔子的数量(斐波那契数列)

    /** * 描述:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子, * 假如兔子都不死,问每个兔子总数为多少? * 分析:根据题目条件可以推断 * 兔子的规律 ...

随机推荐

  1. 【18】什么是FOUC?如何避免

    [18]什么是FOUC?如何避免 Flash Of Unstyled Content: 用户定义样式表加载之前浏览器使用默认样式显示文档,用户样式加载渲染之后再从新显示文档,造成页面闪烁. 解决方法: ...

  2. 三、harbor部署之SSL

    1 签名证书与自签名证书 签名证书:由权威颁发机构颁发给服务器或者个人用于证明自己身份的东西. 自签名证书:由服务器自己颁发给自己,用于证明自己身份的东西,非权威颁发机构发布. 2 openssl简介 ...

  3. php官方微信接口大全

    微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中.内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <?php class ...

  4. EIGRP

    因为rip的收敛时间长  尤其是使用过程中   链路down掉    重收敛的时间比较长  所以在中到大型的园区网中很少用到rip协议 只有在很小的局域网中用到rip   因为收敛时间可能会稍微短一些 ...

  5. NYOJ 293 Sticks

    Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 George took sticks of the same length and cut them r ...

  6. Leetcode 357.计算各个位数不同的数字个数

    计算各个位数不同的数字个数 给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n . 示例: 输入: 2 输出: 91 解释: 答案应为除去 11,22,33 ...

  7. TOJ 5021: Exchange Puzzle

    5021: Exchange Puzzle  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit ...

  8. 分析nginx日志常用的命令总结

    1. 利用grep ,wc命令统计某个请求或字符串出现的次数 比如统计GET /app/kevinContent接口在某天的调用次数,则可以使用如下命令: cat /usr/local/nginx/l ...

  9. 【Luogu】P1613跑路(倍增+Floyd)

    题目链接在此 其实我看到这道题一点想法都没有 设f[i][j][k]表示用2i秒能不能从j走到k.如果可以,那j到k就可以一秒走到,它们的路径长度就是1.方程为f[i][j][k]=f[i-1][j] ...

  10. PAT天梯赛练习题——L3-005. 垃圾箱分布(暴力SPFA)

    L3-005. 垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁 ...