<p>这是从太空学员问题的答案中复制出来的,以帮助rbump将其从“未回答”的问题列表中剔除。</p>
<hr/>
<p>这里的两个关键点是绘制出大量的树,并确保包括第一代和第二代死亡的基本病例检查(两种情况下都是-1,然后是依赖于输入的结果)。</p>
<p>3个潜在病例。常规fib序列当我们不需要解释死亡,第一代和第二代死亡初始化我们的最终序列与复发关系Fn-2+Fn-1-Fn-(monthsAlive+1)</p>
<p>我确信有一种方法可以合并这些检查中的1到2个,使算法更有效,但是到现在为止,它立即正确地解决了一个大型测试用例(90,17)。所以我很高兴。</p>
<p>经验教训:使用整个白板。</p>
<pre><code>#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))
</code></pre>