11 Mortal Fibonacci Rabbits
Problem
Recall the definition of the Fibonacci numbers from “Rabbits and Recurrence Relations”, which followed the recurrence relation Fn=Fn−1+Fn−2Fn=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.
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).
Given: Positive integers n≤100n≤100 and m≤20m≤20.
Return: The total number of pairs of rabbits that will remain after the nn-th month if all rabbits live for mm months.
Sample Dataset
6 3
Sample Output
4
# coding=utf-8
### 11. Mortal Fibonacci Rabbits ### # 0 1 1 2 2 3 4 5 7 9 12 # method1
def fibonacciRabbits(n, m):
F = [0, 1, 1]
for i in range(3, n + 1):
if i <= m:
total = F[i - 1] + F[i - 2]
elif i == m + 1:
total = F[i - 1] + F[i - 2] - 1
else:
total = F[i - 1] + F[i - 2] - F[i - m - 1]
F.append(total)
return (F[n]) # print fibonacciRabbits(6,3) # method2
def f(n, k):
s = [0] * (k + 1) # list *4 [0, 0, 0, 0] s[0]代表当月出生的兔子,s[k]代表当月死亡的兔子
s[0] = 1 # [1, 0, 0, 0]
for x in range(1, n):
s[1:k + 1] = s[0:k]
print s
s[0] = sum(s[2:])
return sum(s[:-1]) print f(10, 3)
11 Mortal Fibonacci Rabbits的更多相关文章
- 指数级计算复杂度 调用Fibonacci函数次数
指数级计算复杂度 计算调用次数 #include <stdio.h> long fibonacciCallTimes(long n); int main(void) { long resu ...
- 赶时髦过了一遍Swift 语言....
Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. ...
- 基础调试命令 - wt (watch and trace)
本文介绍windbg动态调试过程中一个非常有用的命令,wt的用法. wt命令 wt命令之所以称为wt是因为它是watch and trace的简称,即用来观察和跟踪的命令.这个命令一般用在动态调试而不 ...
- [IOS]《A Swift Tour》翻译(一)
以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发 ...
- Apple Swift编程语言入门教程
Apple Swift编程语言入门教程 作者: 日期: 布衣君子 2015.09.22 目录 1 简介 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 ...
- swift 2.x学习笔记(二)
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...
- 来自苹果的编程语言——Swift简介转载】
关于 这篇文章简要介绍了苹果于WWDC 2014发布的编程语言——Swift. 原文作者: Lucida Blog 新浪微博 豆瓣 转载前请保留出处链接,谢谢. 前言 在这里我认为有必要提一下Brec ...
- 《C与指针》第七章练习
本章问题 1.具有空函数体的函数可以作为存根使用,你如何对这类函数进行修改,使其更有用? answer:Have the stub(存根) print out a message when it is ...
- 转 苹果的新编程语言 Swift 简介
苹果官方文档地址 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Pro ...
随机推荐
- 使用bit管理npm包
npm 包给共享,团队写作有好多问题需要解决,类似的解决方案有lerna yarn 使用monorepo,bit 官方有相关的比较 以下只说明如何进行简单的项目使用 网站 https://bitsrc ...
- 使用ntp从时间同步服务器更新centos系统时间的方法
CentOS系统时间同步的步骤如下: 复制代码 代码如下: cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate us.pool.ntp ...
- hadoop启动时,报ssh: Could not resolve hostname xxx: Name or service not known
本文转载自:http://blog.csdn.net/wodewutai17quiet/article/details/76795951 问题:hadoop启动时,报ssh: Could not re ...
- Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次)
ylbtech-Train-Alypay-Cloud:蚂蚁大数据平台培训开课通知(第三次) 1.返回顶部 1. 您好! 很高兴通知您,您已经成功报名将于蚂蚁金服计划在2018年2月28日- 2018年 ...
- 面试总结之C/C++
source code https://github.com/haotang923/interview/blob/master/interview%20summary%20of%20C%20and%2 ...
- [转]Aspose.Words.dll 将 Word 转换成 html
用于网站上,上传 Word 文档后显示文档内容(可看作在线阅读).代码适用于 .net 2.0 或以上版本 (使用的未注册 Aspose.Words.dll 并尝试消除试用标志) 下载地址 strin ...
- sql之强制索引
1.今天我遇到一个问题,在处理百万级数据查询的时候,一般查询会很慢. 2.第一时间想到是建立联合索引,但是数据库存在多条索引的情况下,索引的执行是全部执行. 3.所以这里要按照特定的索引执行,就必须使 ...
- mysql用一个表更新另一个表的方法
Solution 1: 修改1列(navicate可行) update student s, city c set s.city_name = c.name where s.city_code = ...
- 利用U盘安装Redhat-server-Linux-7.1
利用U盘安装Redhat-server-Linux-7.1 [原]红帽 Red Hat Linux相关产品iso镜像下载[百度云]
- 在Ubuntu 16.04上使用bazel交叉编译tensorflow
鸽了这么久,正式开工 Author: carbon email: ecras_y@163.com 参考资料: https://github.com/tensorflow/tensorflow http ...