这个递归函数有什么作用呢?
我在一次采访中得到了这个问题。所以,在我看来,这似乎是一个混乱的斐波那契数列。求和生成器,这给出了一个堆栈溢出。因为 if(n==0) 应该是 if(n<3)
(退出条件错误)。这个问题的准确答案应该是什么?预期的答案是什么?
foo (int n) {
if (n == 0) return 1;
return foo(n-1) + foo(n-2);
}
更新
这个递归函数有什么作用?
看到这3行代码你会想到什么?无需调试。
I got this question in an interview. So, this seems to me a messed up Fibonacci seq. sum generator and this gives a stackoverflow. Because if(n==0) should be if(n<3)
(exit condition is wrong). What should be the precise answer to this question? What was expected as an answer?
foo (int n) {
if (n == 0) return 1;
return foo(n-1) + foo(n-2);
}
UPDATE
What does this recursive function do?
What do you infer when you see these 3 lines of code. No debugging.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,这只是一个递归斐波那契方法,其基本情况不正确(尽管我认为我也不会使用
n <3
...这取决于您如何定义它)。至于“预期的答案是什么”——这取决于问题。您是否被问到了帖子标题中的问题?完全吗?如果是这样,答案是“它会永远递归,直到当你向它传递除 0 以外的任何值时它会炸毁堆栈”——当然还有解释。 (它永远不会结束,因为要么 n-1 不为 0,或 n-2 不为 0,或两者都不是。)
编辑:上面回答了第一个问题。要回答“当你看到这 3 行代码时你会推断出什么”——我会推断相关开发人员从未运行过 0 以外的值的代码,或者不关心代码是否有效。
Yes, it's just a recursive Fibonacci method with an incorrect base case (although I don't think I'd use
n < 3
either... it depends on how you define it).As for "what was expected as an answer" - that would depend on the question. Were you asked exactly the question in the title of your post? If so, the answer is "it recurses forever until it blows up the stack when you pass it any value other than 0" - with an explanation, of course. (It will never end because either n-1 isn't 0, or n-2 isn't 0, or both.)
EDIT: The above answers the first question. To answer "What do you infer when you see these 3 lines of code" - I would infer that the developer in question has never run the code with a value other than 0, or doesn't care about whether or not the code works.
发布的代码的问题是,如果我们评估 foo(1),我们需要找到 foo(0) 和 foo (-1),foo(-1) 然后需要找到 foo(-2) 和 foo(-3)等等。这将继续对 foo() 进行更多调用,直到内存中没有更多空间,从而导致堆栈溢出。对 foo 的调用次数取决于调用堆栈的大小,这将是特定于实现的。
当我看到这些代码行时,我立即得到的印象是,无论编写它的人都没有考虑到可以提供给该函数的所有可能的输入。
为了使递归斐波那契函数不会因 foo(1) 或负输入而失败,我们得到:
编辑:也许负数的返回应该是其他东西,因为斐波那契序列不是为负索引隐式定义的。
然而,如果我们使用 fib(-n) = (-1)^(n+1) fib(n) 的扩展,我们可以获得以下代码:
The problem with the code posted is that if we evaluate foo(1) we need to find foo(0) and foo (-1), foo(-1) then needs to find foo(-2) and foo(-3) and so on. This will keep putting more calls to foo() until there is no more space in the memory resulting in a stack overflow. How many calls to foo are made will depend on the size of the call stack, which will be implementation specific.
When I see these lines of code I immediately get the impression that whoever wrote it hasn't thought about all the possible inputs that could be fed to the function.
To make a recursive Fibonacci function that doesn't fail for foo(1) or a negative input we get:
Edit: perhaps the return for a negative number should be something else, as the fibonacci sequence isn't implicitly defined for negative indexes.
However if we use the extension that fib(-n) = (-1)^(n+1) fib(n) we could get the following code:
假设,您调用 foo ( 1 ),它将有两个子调用 foo(0) 和 foo(-1)。正如你所看到的,一旦到达 foo(-1),n 将无限期地减少,并且永远不会达到终止条件。
准确的答案是:
suppose, you call foo ( 1 ), it will have two sub calls of foo(0) and foo(-1). As you can see, once you get to foo(-1), n will decrease indefinitely and will never reach a terminating condition.
The precise answer would be:
它是一个递归斐波那契函数,第 0 个元素为 1。
Its a recursive fibonacci function with 0th element being 1.
忽略不正确的终止条件,该代码是斐波那契函数的“天真的”递归实现。它的大O时间复杂度非常差。它对于小 n 来说效果很好,但如果 n 大于 50(我只是猜测这个数字),那么它将“永远”运行。
Ignoring the incorrect termination condition, the code is the "naive" recursive implementation of the Fibonacci function. It has very poor big-O time complexity. It would work fine for small n, but if n is greater than say 50 (I'm just guessing that number), then it would take "forever" to run.