Problem

A sequence is an ordered collection of objects (usually numbers), which are allowed to repeat. Sequences can be finite or infinite. Two examples are the finite sequence n-th term of a sequence.

A recurrence relation is a way of defining the terms of a sequence with respect to the values of previous terms. In the case of Fibonacci's rabbits from the introduction, any given month will contain the rabbits that were alive the previous month, plus any new offspring. A key observation is that the number of offspring in any month is equal to the number of rabbits that were alive two months prior. As a result, if Fibonacci sequence having terms  to initiate the sequence). Although the sequence bears Fibonacci's name, it was known to Indian mathematicians over two millennia ago.

When finding the dynamic programming, which successively builds up solutions by using the answers to smaller cases.

Given: Positive integers k≤5.

Return: The total number of rabbit pairs that will be present after  rabbit pairs (instead of only 1 pair).

Sample Dataset

5 3

Sample Output

19

###Rabbits and Recurrence Relations ###
def fib(n,k):
    a, b = 1, 1
    for i in range (2,int(n)):
        a, b = b, int(k)*a + b #只需要保存最近两个月的数量即可
    print  b
    
if __name__ == "__main__":
    fh = open ("C:\\Users\\hyl\\Desktop\\rosalind_fib.txt")
    l = fh.readline().split(' ')
    n, k = l[0], l[1]   
    fib(n,k)

  

相关文章:

  • 2022-12-23
  • 2022-01-27
  • 2021-08-19
  • 2021-12-02
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
猜你喜欢
  • 2021-06-16
  • 2021-12-28
  • 2021-09-02
  • 2021-06-22
  • 2022-01-18
  • 2021-08-05
  • 2021-06-17
相关资源
相似解决方案