所以,我已经看到了针对这个问题或类似问题的一些解决方案,但我真的想知道为什么我的工作不起作用。它比我找到的很多解决方案更容易阅读,所以我很乐意让它发挥作用!
从1对兔开始,2个月后开始繁殖。跑了n个月,兔子在生活了几个月后就死了。 输入'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))
谢谢=]
答案 0 :(得分:2)
这是从SpaceCadets问题的答案中复制的,以帮助将其从“未答复”的问题列表中删除。
这里的两个关键是将树大量抽出来,并确保包含第一代和第二代死亡的基础案例检查(在两种情况下都是-1,然后它取决于输入)。
所以3个潜在的案例。我们不需要考虑死亡时的常规纤维序列,第一代和第二代死亡以初始化我们的最终序列与复发关系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))
答案 1 :(得分:1)
使用递归。
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);
}
答案 2 :(得分:1)
这里有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(仅2个术语, m - 1 )
。
下面的代码(在Python中)的偏移量为2,因为序列开头的初始值为[1,1]。
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
答案 3 :(得分:1)
忽略并发症,一代中兔子对数的等式是
如果我们使用列表,则会出现问题,因为在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
答案 4 :(得分:0)
天真地模拟任务中的行为的代码,我猜可以更快地使用touples
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]))