递归
- #include <stdio.h>
- //递归函数,以最后一人年龄做结束
- int age1(int a) {
- int age2;
- if(a == 1) {
- age2 = 10;
- } else {
- age2 = age1(a - 1) + 3;
- }
- return age2;
- }
- int main(int argc, const char * argv[]) {
- //定义变量求出最后结果
- int age = age1(8);
- printf("第8个人的年龄是%d岁。",age);
- return 0;
- }
复制代码
|
|