#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void recursie(int);

int main(int argc, char **argv) {
  recursie(3);
}

void recursie(int a){
  if(a==0){return;}
    recursie(a-1);
    printf("%d",a);
    recursie(a-1);
}

输出为:1213121有人能解释一下我是怎么得到这个输出的吗?

最佳答案

recursie(3)
  -calls recursie(2)
   - calls recursie(1)
     -calls recursie(0) -> void
     -prints 1
     -calls recursie(0) -> void
   -prints 2
   -calls recursie 1
      -calls recursie 0 -> void
      -prints 1
      -calls recursie 0 -> void
  -prints 3
  -calls recursie(2)
   - calls recursie(1)
     -calls recursie(0) -> void
     -prints 1
     -calls recursie(0) -> void
   -prints 2
   -callse recurse 1
      -calls recusie 0 -> void
      -prints 1
      -calls recursie 0 -> void
end

关于c - C中的递归函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12122504/

10-11 16:16