[英]How many times is this function called?
I have this subalgorithm on recursion and I know that it's called 5 times, but I don't understand why.我有这个关于递归的子算法,我知道它被调用了 5 次,但我不明白为什么。 The initial values are m=1, n=2.初始值为 m=1,n=2。
I've thought the function is called for 7 times.我认为该函数被调用了 7 次。
int Ack(int m, int n)
{
if (m==0)
return n+1;
else
{
if (m>0 && n==0)
return Ack(m-1,1);
else
return Ack(m-1,Ack(m,n-1));
}
}
I expect that it's called 7 times or even more, but the actual number is 5.我预计它会被调用 7 次甚至更多,但实际数量是 5。
This how 7 is arrived at: 7 是这样得出的:
您重复计算了 2 个调用:#7 与 #4 相同,#8 与 #2 相同(仅显示Ack
返回的值):“回来”与调用不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.