C 练习实例28

C语言教程评论

C 练习实例28

题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。。文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili28.html

实例

#include <stdio.h>

int age(n)
int n;
{
    int c;
    if(n==1) c=10;
    else c=age(n-1)+2;
    return(c);
}
int main()
{
    printf("%d\n",age(5));
}

以上实例输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili28.html

18
文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili28.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-lianxishili28.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 最后更新:2021-12-7
公式库网
  • 本文由 公式库网 发表于 2021年12月6日20:12:40
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-lianxishili28.html

发表评论