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.
  • 相关阅读:
    C# 操作ACCESS数据库
    装饰模式(Decorator Pattern)
    桥接模式(Bridge Pattern)
    单件模式(Singleton Pattern)
    横竖不能重复的9个数,,,,,
    C#操作Access数据库的例子
    组合模式(Composite Pattern)
    建造者模式(Builder Pattern)
    工厂方法模式(Factory Method)
    外观模式(Façade Pattern)
  • 原文地址:https://www.cnblogs.com/zhangshengdong/p/9857034.html
Copyright © 2011-2022 走看看