zoukankan      html  css  js  c++  java
  • C语言之递归

    递归例子如下:

     1 #include <stdio.h>
     2 /*函数声明*/
     3 void digui(int n);
     4 
     5 int main()
     6 {
     7   int n=10;
     8   digui(n);
     9   return 0;
    10 }
    11 
    12 void digui(int n)
    13 {
    14   printf("level1-value of %d
    ",n);
    15   if(n>2){
    16     digui(n-1);
    17   } 
    18   printf("level2-value of %d
    ",n);
    19 }

    程序结果如下:

    [zsd@TOMCAT ~]$ ./test03
    level1-value of 10
    level1-value of 9
    level1-value of 8
    level1-value of 7
    level1-value of 6
    level1-value of 5
    level1-value of 4
    level1-value of 3
    level1-value of 2
    ---------------------------邪恶的分割线------------------------
    level2-value of 2
    level2-value of 3
    level2-value of 4
    level2-value of 5
    level2-value of 6
    level2-value of 7
    level2-value of 8
    level2-value of 9
    level2-value of 10

    通过gdb的调试,对代码的16行和18行设置断点,gdb执行的效果如下:

    (gdb) run
    Starting program: /home/zsd/test03debug 
    level1-value of 10
    
    Breakpoint 1, digui (n=10) at test03.c:16
    16          digui(n-1);                  //开始第一次向下递归,递归数为9
    (gdb) 
    (gdb) continue
    Continuing.
    level1-value of 9
    
    Breakpoint 1, digui (n=9) at test03.c:16
    16          digui(n-1);                 //向下递归,递归数为8
    (gdb) continue
    Continuing.
    level1-value of 8
    
    Breakpoint 1, digui (n=8) at test03.c:16
    16          digui(n-1);
    (gdb) continue
    Continuing.
    level1-value of 7
    
    Breakpoint 1, digui (n=7) at test03.c:16
    16          digui(n-1);
    (gdb) continue
    Continuing.
    level1-value of 6
    
    Breakpoint 1, digui (n=6) at test03.c:16
    16          digui(n-1);
    (gdb) continue
    Continuing.
    level1-value of 5
    
    Breakpoint 1, digui (n=5) at test03.c:16
    16          digui(n-1);
    (gdb) continue
    Continuing.
    level1-value of 4
    
    Breakpoint 1, digui (n=4) at test03.c:16
    16          digui(n-1);
    (gdb) continue
    Continuing.
    level1-value of 3
    
    Breakpoint 1, digui (n=3) at test03.c:16
    16          digui(n-1);                 //一直到这里,递归数为2.这个时候,上面递归的每一个函数digui(2),digui(3)....digui(10)有最后一条printf("level2-value of %d
    ",n);语句没有执行。
    (gdb) continue
    Continuing.
    level1-value of 2
    
    Breakpoint 2, digui (n=2) at test03.c:18   //digui(2)执行printf("level2-value of %d
    ",n);语句
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 2
    
    Breakpoint 2, digui (n=3) at test03.c:18   //digui(3)执行printf("level2-value of %d
    ",n);语句
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 3
    
    Breakpoint 2, digui (n=4) at test03.c:18   //以上述递推,一直到digui(10)执行完毕。
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 4
    
    Breakpoint 2, digui (n=5) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 5
    
    Breakpoint 2, digui (n=6) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 6
    
    Breakpoint 2, digui (n=7) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 7
    
    Breakpoint 2, digui (n=8) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 8
    
    Breakpoint 2, digui (n=9) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 9
    
    Breakpoint 2, digui (n=10) at test03.c:18
    18        printf("level2-value of %d
    ",n);
    (gdb) continue
    Continuing.
    level2-value of 10
    
    Program exited normally.
  • 相关阅读:
    CocosIDE导出Android APK的注意事项
    C++14尝鲜:Generic Lambdas(泛型lambda)
    silverlight调用WebService传递json接收绑定数据
    解决考试系统高并发数据载入不对问题
    汇编入门学习笔记 (九)—— call和ret
    Java SerialPort SDK
    how tomcat works 总结 二
    linux下多线程的调试
    垃圾回收GC:.Net自己主动内存管理 上(二)内存算法
    HDU-4973-A simple simulation problem.(线段树)
  • 原文地址:https://www.cnblogs.com/zhangshengdong/p/9857034.html
Copyright © 2011-2022 走看看