c语言谭浩强第七章例题

1、用递归求学生年龄

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int age(int n);
 5     printf("NO.5 age is %d\n",age(5));
 6     return 0;
 7 }
 8 int age(int n)
 9 {
10     int c;
11     if(n==1)
12         c=10;
13     else
14         c=age(n-1)+2;
15     return c;
16 }

2、求阶乘

#include<stdio.h>
int main()
{
    int fac(int n);
    int n,y;
    printf("输入n: ");
    scanf("%d",&n);
    y=fac(n);
    printf("%d!=%d\n",n,y);
    return 0;
}
int fac(int n)
{
    int f;
    if(n==1||n==0)
        f=1;
    else
        f=fac(n-1)*n;
    return f;
}

3、汉诺塔问题

 1 #include<stdio.h>
 2 int main()
 3 {
 4     void hanoi(int n,char one,char rwo,char three);
 5     int m;
 6     printf("input the number of diskes:");
 7     scanf("%d",&m);
 8     printf("the step to move %d diskes:\n",m);
 9     hanoi(m,'A','B','C');
10     return 0;
11 }
12 void hanoi(int n,char one,char two,char three)
13 {
14     void move(char x,char y );
15     if(n==1)
16         move(one,three);
17     else
18     {
19         hanoi(n-1,one,three,two);
20         move(one,three);
21         hanoi(n-1,two,one,three);
22     }
23 }
24 void move(char x,char y)
25 {printf("%c-->%c          ",x,y);
26 }

 

转载于:https://www.cnblogs.com/1998wdq/p/11348303.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值