英文:
How does the control flow in a recursive program work?
问题
我不确定为什么这段代码会打印出"5678998765"。我明白它是如何得到"56789"部分的,但当数字开始递减时,我感到困惑。所以如果有人能解释一下,我将不胜感激。
int out(int k) {
if (k == 10) {
return 1;
}
printf("%d", k);
out(k + 1);
printf("%d", k);
}
int main() {
out(5);
return 0;
}
英文:
I am not sure why this piece of code prints out "5678998765". I see how it gets the 56789 part but when the numbers start going down is where i get confused. So if someone could explain i would be grateful.
int out(int k) {
if (k == 10) {
return 1;
}
printf("%d", k);
out(k +1);
printf("%d", k);
}
int main()
{
out(5);
return 0;
答案1
得分: 1
这是从上一个 printf
输出的;每个函数都需要一直执行到结束。
int out(int k) {
if (k == 10) {
return 1;
}
printf(" + %d", k);
out(k +1);
printf(" - %d", k);
}
你应该尝试这个,当它会更清晰时。
英文:
It is printed from last printf
; every function needs to be executed till the end.
int out(int k) {
if (k == 10) {
return 1;
}
printf(" + %d", k);
out(k +1);
printf(" - %d", k);
}
You should try this, when it will be clearer.
答案2
得分: 0
在每个不等于10
的k
值的函数内部,都有两次调用printf
。
printf("%d", k);
out(k + 1);
printf("%d", k);
因此,如果在第一次调用函数时k
等于5,那么输出将是:
5 (调用out(6)) 5
然后在递归调用中,输出将是:
56 (调用out(7)) 65
以此类推。
当k
等于10
时,将不会有任何输出。
英文:
Within the function for each value of k
that is not equal to 10
there are two calls of printf
printf("%d", k);
out(k +1);
printf("%d", k);
So if the function is called the first time when k is equal to 5 then you will have
5 ( a call of out( 6 ) ) 5
then in the recursive call you will heav
56 ( a call of out( 7 ) ) 65
and so on.
When k
is equal to 10
then nothing is outputted.
答案3
得分: 0
当main
评估out(5)
时,out
运行并执行以下操作:
k
不等于10,所以继续。- 打印
k
,即5。 - 评估
out(6)
。 - 打印
k
,即5。
这评估了out(6)
,所以将步骤“评估out(6)
”替换为out
执行的操作:
k
不等于10,所以继续。- 打印
k
,即5。k
不等于10,所以继续。- 打印
k
,即6。 - 评估
out(7)
。 - 打印
k
,即6。
- 打印
k
,即5。
现在你可以看到程序将打印5,然后6,然后评估out(7)
,然后打印6,然后打印5。这一直持续下去,直到:
k
不等于10,所以继续。- 打印
k
,即5。k
不等于10,所以继续。- 打印
k
,即6。k
不等于10,所以继续。- 打印
k
,即7。k
不等于10,所以继续。- 打印
k
,即8。k
不等于10,所以继续。- 打印
k
,即9。k
等于10,所以返回1。
- 打印
k
,即9。
- 打印
k
,即8。
- 打印
k
,即7。
- 打印
k
,即6。
- 打印
k
,即5。
英文:
When main
evaluates out(5)
, out
runs and does this:
k
is not 10, so continue.- Print
k
, which is 5. - Evaluate
out(6)
. - Print
k
, which is 5.
That evaluates out(6)
, so replace the step “Evaluate out(6)
” with what out
does for that:
k
is not 10, so continue.- Print
k
, which is 5.k
is not 10, so continue.- Print
k
, which is 6. - Evaluate
out(7)
. - Print
k
, which is 6.
- Print
k
, which is 5.
Now you can see the program is going to print 5, then 6, then evaluate out(7)
, then print 6, then print 5. This continues until we have:
k
is not 10, so continue.- Print
k
, which is 5.k
is not 10, so continue.- Print
k
, which is 6.k
is not 10, so continue.- Print
k
, which is 7.k
is not 10, so continue.- Print
k
, which is 8.k
is not 10, so continue.- Print
k
, which is 9.k
is 10, so return 1.
- Print
k
, which is 9.
- Print
k
, which is 8.
- Print
k
, which is 7.
- Print
k
, which is 6.
- Print
k
, which is 5.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论