版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
题目:
有5个学生坐在一起,问第5个学生多少岁,他说比第4个学生大2岁。问第4个学生岁数,他说比第3个学生大2岁。
问第3个学生,又说比第2个学生大2岁。问第2个学生,说比第1个学生大2岁。最后问第1个学生,他说是10岁。
请问第5个学生多大
代码:
#include <stdio.h>
int GetStudentAge(int seq);
int main()
{
printf("the fifth student age is %d \r\n", GetStudentAge(5));
return 0;
}
int GetStudentAge(int seq)
{
if(1 == seq)
{
return 10;
}else
{
return (GetStudentAge(seq - 1) + 2);
}
}
输出:
[root@localhost test]# ls
main.cpp
[root@localhost test]# g++ -o demo main.cpp
[root@localhost test]# ls
demo main.cpp
[root@localhost test]# ./demo
the fifth student age is 18
[root@localhost test]#