递归程序中的控制流如何工作?

huangapple go评论58阅读模式
英文:

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

在每个不等于10k值的函数内部,都有两次调用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.

huangapple
  • 本文由 发表于 2023年2月6日 21:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定