如果“LINE 3”为0,fib(n)需要多少次附加函数调用被删除了?

发布于 2024-08-25 04:27:57 字数 249 浏览 10 评论 0原文

我刚刚在面试中遇到这个问题,不知道如何计算答案。
如果删除“LINE 3”,fib(n) 需要多少次附加函数调用?答案应该用n 表示。

int fib(int n) {
  if(n == 0) return 0;
  if(n == 1) return 1;
  if(n == 2) return 1; //LINE 3 HERE <---

  return fib(n - 1) + fib(n - 2);
}

I just got this question on an interview and had no idea how to calculate the answer.
How many additional function calls does fib(n) require if "LINE 3" is removed? The answer should be in terms on n.

int fib(int n) {
  if(n == 0) return 0;
  if(n == 1) return 1;
  if(n == 2) return 1; //LINE 3 HERE <---

  return fib(n - 1) + fib(n - 2);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-09-01 04:27:57

它可以很容易地计算出来。旧代码:

TO(0)=TO(1)=TO(2)=1
TO(n)=TO(n-1)+TO(n+2)+1

新代码:

TN(0)=TN(1)=1
TN(n)=TN(n-1)+TN(n-2)+1

只需将两者相减即可计算差异:

D(0)=D(1)=0
D(2)=3-1=2
D(n)=TN(n)-TO(n)=TN(n-1)+TN(n-2)+1-(TO(n-1)+TO(n+2)+1)
    =(TN(n-1)-TO(n-1))+(TN(n-2)-TN(n-2))+(1-1)
    =D(n-1)+D(n-2)

这意味着差异是从 0,0,2 开始的斐波那契数列。也可以为其计算封闭形式表达式。

It can be easily calculated. The old code:

TO(0)=TO(1)=TO(2)=1
TO(n)=TO(n-1)+TO(n+2)+1

The new code:

TN(0)=TN(1)=1
TN(n)=TN(n-1)+TN(n-2)+1

The difference is computed simply by subtracting those two:

D(0)=D(1)=0
D(2)=3-1=2
D(n)=TN(n)-TO(n)=TN(n-1)+TN(n-2)+1-(TO(n-1)+TO(n+2)+1)
    =(TN(n-1)-TO(n-1))+(TN(n-2)-TN(n-2))+(1-1)
    =D(n-1)+D(n-2)

Which means the difference is a Fibonacci sequence beginning with 0,0,2. It is also possible to calculate a closed form expression for it.

对不⑦ 2024-09-01 04:27:57

所需的额外调用次数也是斐波那契序列。

0 0 2 2 4 6 10 16 26 42 68 110 178 288 466

#include<iostream>
using namespace std;

int a = 0;
int b = 0;

int fib(int n) {
    a++;
  if(n == 0) return 0;
  if(n == 1) return 1;
  if(n == 2) return 1; //LINE 3 HERE <---

  return fib(n - 1) + fib(n - 2);
} 

int fib1(int n) {
    b++;
  if(n == 0) return 0;
  if(n == 1) return 1;

  return fib1(n - 1) + fib1(n - 2);
}

int main(int argc, char* argv[])
{
    for(int i =0 ;i<15;i++)
    {
        fib(i);
        fib1(i);

        cout<<b-a<<" ";

        b = a = 0;
    }
}

注意:我认为这会是一些常数,但是......

Number of extra calls required is also Fibonacci kind of sequence.

0 0 2 2 4 6 10 16 26 42 68 110 178 288 466

#include<iostream>
using namespace std;

int a = 0;
int b = 0;

int fib(int n) {
    a++;
  if(n == 0) return 0;
  if(n == 1) return 1;
  if(n == 2) return 1; //LINE 3 HERE <---

  return fib(n - 1) + fib(n - 2);
} 

int fib1(int n) {
    b++;
  if(n == 0) return 0;
  if(n == 1) return 1;

  return fib1(n - 1) + fib1(n - 2);
}

int main(int argc, char* argv[])
{
    for(int i =0 ;i<15;i++)
    {
        fib(i);
        fib1(i);

        cout<<b-a<<" ";

        b = a = 0;
    }
}

NOTE: I thought it would be some constant but...

雅心素梦 2024-09-01 04:27:57

假设没有第 3 行并计算 f(3):

f(3) = f(2) + f(1)
f(1) = 1
f(2) = f(1) + f(0)
f(0) = 0
f(1) = 1

现在需要 3 次调用来计算 f(2)。如果有第 3 行,那么这将在 1 次调用中完成。

该算法的复杂度(没有第三行)是O(2^n)。当您添加第 3 行时,其中包含 n = 2 情况的显式解决方案,复杂度变为 O(2^(n-1)) 等于 (1/2) * O(2^n) = kO(2^n) 其中系数 k = 0.5。如果您为 n = 3 的情况添加显式解,那么您将得到 k = 0.25 等等。当您添加 p 显式解决方案时,复杂度将为:

    1
O (--- * 2^n)
   2^p 

这意味着,如果您计算从 1 到 n 的 n 的答案,并且保存所有计算出的解决方案,那么您将得到 p = n - 1 并且每个 n 步骤的算法和复杂度将为 2*O(n)

Let's assume that there's no 3rd line and calculate f(3):

f(3) = f(2) + f(1)
f(1) = 1
f(2) = f(1) + f(0)
f(0) = 0
f(1) = 1

It takes 3 calls to calculate f(2) now. It there was a 3rd line then this will be done in 1 call.

The complexity of this algorithm (without 3rd line) is O(2^n). When you add line 3, which contain explicit solution for the case when n = 2 the complexity becomes O(2^(n-1)) what is equals to (1/2) * O(2^n) = kO(2^n) where koefficient k = 0.5. If you add explicit solution for the case where n = 3 then you get k = 0.25 and so on. When you add p explicit solutions the complexity will be:

    1
O (--- * 2^n)
   2^p 

This means that if you will calculate the answer for n from 1 to n and if you'll save all calculated solutions then you'll get p = n - 1 and the algorithm for each of n steps and the comlexity will be 2*O(n).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文