Python 斐波那契兔在任意数月后死亡
所以,我已经看到了一些解决这个问题或类似问题的方法,但我真的想知道为什么我的方法不起作用。它比我找到的许多解决方案更容易阅读,所以我很想让它发挥作用 从1对兔子开始,2个月后开始繁殖。跑n个月,兔子活了m个月就死了。 “6 3”的输入应返回4,但它返回3Python 斐波那契兔在任意数月后死亡,python,fibonacci,rosalind,Python,Fibonacci,Rosalind,所以,我已经看到了一些解决这个问题或类似问题的方法,但我真的想知道为什么我的方法不起作用。它比我找到的许多解决方案更容易阅读,所以我很想让它发挥作用 从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, separa
#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))
运行n个月,兔子在m个月后死亡。
n、 m=输入(“输入要运行的月份,以及兔子活了多少个月,用空格分隔”).split()
n、 m=int(n),int(m)
世代=[1,1,2]#用1对种子播种序列,然后在繁殖月份播种。
def fib(i,j):
计数=3#我们从第三代开始。
而(计数
谢谢=]这是从太空学员问题中的答案复制过来的,以帮助rbump将其从“未回答”的问题列表中剔除
这里的两个关键点是绘制大量的树,并确保包括第一代和第二代死亡的基本病例检查(两种情况下都是-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))
运行n个月,兔子在m个月后死亡。
n、 m=输入(“输入要运行的月份,以及兔子活了多少个月,用空格分隔”).split()
n、 m=int(n),int(m)
世代=[1,1]#用1对种子对序列进行播种,然后在繁殖月份进行。
def fib(i,j):
计数=2
而(计数使用递归
public static int fibRec(int months, int dieAfter) {
if(months <= 0) return 0;
if(months == 1) return 1;
if(months <= dieAfter)
return fibRec(months-1, dieAfter) + fibRec(months-2, dieAfter);
else if (months == dieAfter+1)
return fibRec(months-1, dieAfter) + fibRec(months-2, dieAfter) - 1;
else
return fibRec(months-1, dieAfter) + fibRec(months-2, dieAfter)
- fibRec(months-(dieAfter+1), dieAfter);
}
public static int fibRec(int月,int日后){
如果(月这里有2例复发关系。考虑到n是序列运行的月数,m是一对夫妇的生存月数:
1) 如果序列中的索引(从零开始)小于m:
正常斐波那契(当前项=前一项+前一项)
2) 如果索引大于或等于m:
当前项=先前项(m-1)的总和(忽略之前的项)。
下面是一个名为a和m=5的序列示例:
A5=A0+A1+A2+A3(4个术语,即m-1,忽略前一个术语)
.
如果m=3则:
A3=A0+A1(只有两个术语,m-1)
下面的代码(在Python中)由于序列开头的初始[1,1]而偏移了2
def mortal_rabbits(n, m):
sequence = [1, 1]
for i in range(n - 2):
new_num = 0
if i + 2 < m:
#Normal fibonacci - No deaths yet
new_num = sequence[i] + sequence[i + 1]
else:
#Different reoccurence relation - Accounting for death
for j in range(m - 1):
new_num += sequence[i - j]
sequence.append(new_num)
return sequence
def凡人兔(n,m):
序列=[1,1]
对于范围(n-2)内的i:
新数量=0
如果i+2
忽略并发症,一代兔子对数的方程式为
如果使用列表,则会出现问题,因为当n==m
时,我们需要位于-1
位置的值,这显然超出了范围
def rabbit_pairs(n, m):
sequence = list()
for i in range(n):
if i < 2:
# Normal Fibonacci initialization
total = 1
sequence.append(total)
elif (i < m) or (m == 0):
# Normal Fibonacci calculation
total = sequence[i - 1] + sequence[i - 2]
sequence.append(total)
elif i == m:
# Now we need R(n - (m + 1)), but i - (m + 1) < 0, so we have to
# provide the missing value
total = sequence[i - 1] + sequence[i - 2] - 1
sequence.append(total)
else:
# i - (m + 1) >= 0, so we can get the value from the sequence
total = sequence[i - 1] + sequence[i - 2] - sequence[i - (m + 1)]
sequence.append(total)
return total
def兔_对(n,m):
顺序=列表()
对于范围(n)中的i:
如果i<2:
#正规斐波那契初始化
总计=1
sequence.append(总计)
elif(i=0,因此我们可以从序列中获得值
总计=序列[i-1]+se
def fibm(n,m):
seq = [[0,0],[0,1],[1,0],[1,1]]
#(old,new)
for i in range(4,n+1):
new = seq[i-1][0]#new = previous old
old = seq[i-1][0] + seq[i-1][1] #old = prev old + prev new
if i>m:
old = old - seq[i-m][1] #new from m ago die off
seq.append([old,new])
return(seq)
n,m = 95,16
print(sum(fibm(n,m)[-1]))