问题:
5个人坐在一起,第5个人说他比第四个大2岁,第4个说比第三个大2岁,第3个说比第二个大2岁, 第2个说比第1个大2岁,第一个说自己10岁,当输入第几个人的时候求出其对应的年龄。
#include <stdio.h>
#include <stdlib.h>
int age(int n);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int n, k;
printf("input n:");
scanf("%d", &n);
k = age(n);
printf("%d", k);
return 0;
}
int age(int n)
{
int x;
if(n==1)
x = 10;
else
x = age(n-1) + 2;//调用递归
return x;
}
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/44871611