Problem

  1. Problem
  2. Recall the definition of the Fibonacci numbers from “Rabbits and Recurrence Relations”, which followed the recurrence relation Fn=Fn−1+Fn−2 and assumed that each pair of rabbits reaches maturity in one month and produces a single pair of offspring (one male, one female) each subsequent month.
  3. Our aim is to somehow modify this recurrence relation to achieve a dynamic programming solution in the case that all rabbits die out after a fixed number of months. See Figure 4 for a depiction of a rabbit tree in which rabbits live for three months (meaning that they reproduce only twice before dying).

兔子繁殖问题

假定如下,

  1. 种群在第一个月开始时有一对新生兔子。
  2. 一个月后兔子达到生育年龄。
  3. 在任何给定的月份,每只育龄兔子都会与另一只育龄兔子交配。
  4. 两只兔子交配一个月后,它们会生出一只雄性兔子和一只雌性兔子。
  5. 兔子有n个月的寿命

问题:
input: 对于给定的月份n,兔子的寿命k, 每次死亡的兔子数量m
output: 兔子总对数

  1. n = 6, rabbit_life = 3, rabbit_die_num = 1
  2. output: 4

思路:

  • 利用递归解决 兔子在寿终正寝前的数量符合斐波那契数列,在出现死亡的第一个月时,每个月都会有m只兔子死亡,减去相应的死亡数量即可

    solution -1

    ```python def rabbit_fab2(n, rabbit_life = 3, rabbit_die_num = 1): if n <= 2:
    1. return 1
    if n > 2 and n <= rabbit_life:
    1. return rabbit_fab2(n - 2) + rabbit_fab2(n - 1)
    else:
    1. return rabbit_fab2(n - 2) + rabbit_fab2(n - 1) - rabbit_die_num

def main(): rabbit_fab2(n = 6)

if name == ‘main‘ main() ```