11

所以,我已经看到了一些解决这个问题或类似问题的方法,但我真的很想知道为什么 我的不起作用。它比我找到的许多解决方案更容易阅读,所以我很乐意让它工作!

从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))

谢谢=]

4

5 回答 5

2

这是从 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))
于 2013-09-03T01:08:48.827 回答
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);
}
于 2015-10-27T19:56:57.050 回答
1

这里的递归关系有2种情况。考虑到n是序列运行的月数,m是一对将要生存的月数:

1) 如果序列中的索引(从零开始)小于m
正常斐波那契(当前项 = 前一项 + 前一项)。

2)如果索引大于或等于m
当前术语 = ( m - 1 ) 先前术语的总和(忽略之前的术语)。

这是一个名为Am = 5的序列的示例:
A5 = A0 + A1 + A2 + A3 (4 个术语, m - 1,忽略之前的那个)

如果m = 3则:
A3 = A0 + A1 (只有 2 项,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
于 2016-01-20T13:31:28.887 回答
1

忽略复杂性,一代中兔子对数的等式是

rabbit_pairs

如果我们使用列表,就会出现问题,因为当 时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
于 2016-02-26T06:50:57.113 回答
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]))
于 2021-03-09T13:25:38.380 回答