码迷,mamicode.com
首页 > 其他好文 > 详细

函数的递归

时间:2018-02-02 21:55:07      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:scanf   void   必须   style   color   自己   blog   col   个数   

函数可以调用自己,叫递归

函数必须要有终止条件

#include<stdio.h>

void test(int n)
{
    printf("n = %d\n", n);   //先序递归 
    if(n < 10)   //终止条件 
    {
        test(n + 1);
    }
    printf("n = %d\n", n);   //后序递归 
 } 

int age(int n)
{
    int i;
    if(n == 1)
      return 10;  //最后一个人的年龄 
    return age(n - 1) + 2;       
}

int to_binary(unsigned int n)
{
    int i = n % 2;  //取余 
    if(n >= 2 )
    to_binary(n / 2);  
    printf("%d", i);   //后序递归 
}

int main()
{
    int a = 13;
    scanf("%d", &a);   //输入一个数 
    to_binary(a);
    printf("\n");
    //test(a);
    //printf("n = %d\n", age(a));
    return 0;
}

 

函数的递归

标签:scanf   void   必须   style   color   自己   blog   col   个数   

原文地址:https://www.cnblogs.com/yangxiaoqin/p/8406862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!