小程序_递归求年纪

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

#include <stdio.h>

int age(int num);

void main(void)
{

int result = 0;

result = age(5);
printf("The age of 5th person is %d ",result);

}

int age(int num)
{
int result = 0;

if(1==num)
{
result = 10;
}
else
{
result = 2+age(num-1);
}
return result;

}

/***************** 今天为了更好的明天 ******************/
原文地址:https://www.cnblogs.com/cheng-amy/p/5809609.html