gpt4 book ai didi

c - 递归和返回维护

转载 作者:行者123 更新时间:2023-11-30 19:56:27 26 4
gpt4 key购买 nike

while doing recursion how many stacks are maintained inside for example in C program of factorial ?
Is queue also maintained internally for recursion?

Function Implementation:

if(x==1){

return 1;

}
else{

return x*facto(x-1);
}

如果 Stack 为 1 并且每一帧由 x*(x-1) 组成,直到 x==1 那么返回值如何乘以先前的值..

in more simple way lets take a stack -->


returns
|2(1)|----> 2(1) evaluates to 2
|3(2)|----> 3(2)<______________| evaluates to 6
|4(3)|----> 4(6)<______________| evaluates to 24
|5(4)|----> 5*(24)<____________| evaluates to 120
------ finally back to main...

正如我之前所说,如何维护返回,因为要恢复之前的计算值,应该在内部维护,对吗? (或传给继任者)

最佳答案

你只有一堆。堆栈上会有多个帧,每次调用该函数都会添加一个新帧。

编辑:至少在没有更多可用的堆栈空间之前,您将得到一个 StackOverflow :)

关于c - 递归和返回维护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27926381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com