したがって、この問題または同様の問題に対するいくつかの解決策を見てきましたが、なぜ 私のものが機能しないのかを本当に知りたいです。私が見つけた多くの解決策よりもはるかに読みやすいので、うまく機能させたいです!
2か月後に繁殖を開始する1組のウサギから始めます。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))
ありがとう=]