- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以,我已经看到了针对这个问题或类似问题的一些解决方案,但我真的很想知道为什么我的解决方案不起作用。它比我找到的许多解决方案更容易阅读,所以我很乐意让它发挥作用!
从 1 对兔子开始,它们将在 2 个月后开始繁殖。跑了 n 个月,兔子在活了 m 个月后就死了。输入 '6 3' 应该返回 4,但它返回 3。
#run for n months, rabbits die after m months.
n, m = input("Enter months to run, and how many months rabbits live, separated by a space ").split()
n, m = int(n), int(m)
generations = [1, 1, 2] #Seed the sequence with the 1 pair, then in their reproductive month.
def fib(i, j):
count = 3 #we start at the 3rd generation.
while (count < i):
if (count < j):
generations.append(generations[count-2] + generations[count-1]) #recurrence relation before rabbits start dying
else: #is just the fib seq (Fn = Fn-2 + Fn-1)
generations.append((generations[count-2] + generations[count-1]) - generations[(count-j)]) #Our recurrence relation when rabbits die every month
count += 1 #is (Fn = Fn-2 + Fn-1 - Fn-j)
return (generations[count-1])
print (fib(n, m))
print ("Here's how the total population looks by generation: \n" + str(generations))
谢谢=]
最佳答案
这是从 SpaceCadets 问题的答案中复制过来的,以帮助将其从“未回答”的问题列表中剔除。
这里的两个关键是将树绘制成大量,并确保包括对第一代和第二代死亡的基本情况检查(在两种情况下都是 -1,然后它取决于输入).
所以 3 个潜在案例。当我们不需要考虑死亡时的常规 fib 序列,第一代和第二代死亡用递归关系 Fn-2 + Fn-1 - Fn-(monthsAlive+1) 初始化我们的最终序列
我确信有一种方法可以合并这些检查中的 1 或 2 个并使算法更高效,但截至目前它立即正确地解决了一个大型测试用例 (90、17)。所以我很高兴。
经验教训:使用整个白板。
#run for n months, rabbits die after m months.
n, m = input("Enter months to run, and how many months rabbits live, separated by a space ").split()
n, m = int(n), int(m)
generations = [1, 1] #Seed the sequence with the 1 pair, then in their reproductive month.
def fib(i, j):
count = 2
while (count < i):
if (count < j):
generations.append(generations[-2] + generations[-1]) #recurrence relation before rabbits start dying (simply fib seq Fn = Fn-2 + Fn-1)
elif (count == j or count == j+1):
print ("in base cases for newborns (1st+2nd gen. deaths)") #Base cases for subtracting rabbit deaths (1 death in first 2 death gens)
generations.append((generations[-2] + generations[-1]) - 1)#Fn = Fn-2 + Fn-1 - 1
else:
generations.append((generations[-2] + generations[-1]) - (generations[-(j+1)])) #Our recurrence relation here is Fn-2 + Fn-1 - Fn-(j+1)
count += 1
return (generations[-1])
print (fib(n, m))
print ("Here's how the total population looks by generation: \n" + str(generations))
关于python - 任意数月后斐波那契兔子死亡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310051/
我想要以下内容: void foo( /* something representing a function f */, /* arguments a1, a2, etc. in s
简而言之,我想声明一个这样的特征: trait Test { def test(amount: Int): A[Int] // where A must be a Monad } 这样我就可以
在 GWT 中,如何在 onModuleLoad 方法中插入框架集以及相对嵌套的框架集和框架,以合并许多小程序和其他小部件和 HTML?代码片段是: 公共(public)类 MainEntryPoin
这个问题在这里已经有了答案: How do I best simulate an arbitrary univariate random variate using its probability
我对java相当陌生,并且习惯于枚举本质上只不过是一个命名的整数列表。 现在我正在编写一个实现,其中父类有几个采用枚举值作为参数的方法。枚举将在子类中定义,并且会略有不同。由于枚举基本上看起来像类,所
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
想象一下 6-7 台服务器的设置都完全相同Java 版本“1.6.0_18”OpenJDK 运行时环境 (IcedTea6 1.8) (fedora-36.b18.fc11-i386)OpenJDK
这个问题在这里已经有了答案: What are some uses of template template parameters? (10 个答案) 关闭 4 年前。 我有一个根据策略舍入值的函数
我正在寻找如何在 Java 中给定一个 Async CompletableFutures 列表,以便前 N 个中的任何一个成功完成或失败。除非没有 N 次成功,否则忽略任何失败。 有这方面的例子吗?
我面临的问题是项目已经使用集群编程来分配任务。 if (cluster.isMaster) { // Fork workers. for (var i = 0; i { }); } el
我正在为 Luxology modo(3D 和 VFX 应用程序)编写脚本,该脚本使用 python 作为脚本语言。在我的脚本中的某个位置,我正在读取从其他应用程序输出的文本文件,并从该文本文件的行创
这个问题在这里已经有了答案: Fast arbitrary distribution random sampling (inverse transform sampling) (5 个答案) 关闭
我只是遇到了一个问题,我有一个结构数组,例如 package main import "log" type Planet struct { Name string `json:"
我正在尝试将 class ResponseResult 编码为 json case class ResponseResult (var Code : Int, var
我想将一个矩阵中的一个 block 复制到另一个矩阵的一部分中。要将其与任何类型的 n 维数组一起使用,我需要通过 [] 运算符应用带有偏移量的列表。有办法做到这一点吗? mat_bigger[0:5
我有一个匹配一组数字和字母的正则表达式。但是我希望能够排除任何三个连续的字母。这是为了防止意外形成单词或缩写。 我的表达如下。它还排除了一些类似的字符,如 0、o、O 和 1、i、I、l): ^[2-
根据documentation . 应匹配任何字符,但不匹配重音字符。 mysql> select 'test' regexp 't.st'; +----------------------+ | '
我该如何用 JavaScript 编写这个 if 语句? if(url == "http://www.google.com/" && "*") { ... } * 需要灵活并接受添加到第一个变量上
我知道 cPython 有一个 GIL,因此如果不使用多处理模块,您的脚本就无法在多个内核上运行。但是有什么可以阻止内置功能,例如使用多核进行排序吗?我不了解 cPython 结构,但我想我要问的问题
寻找命令行 gdb 的替代方法来检查 OSX 上的核心转储 - 有没有办法让 Xcode 打开带有调试符号的任意核心转储? 最佳答案 您是否尝试过使用 MachOView 1? 听起来它可能适用于查看
我是一名优秀的程序员,十分优秀!