求問這樣做錯在哪里
#include <stdio.h>?
int getAge(int n){
? ? int age; //定義第n個人的歲數(shù)
? ? if (n==1){
? ? ? ? return 10;
? ? }
? ? else{
? ? ? ? age=getAge(n)+2;
? ? ? ? return age;
? ? }
}
int main()?
{
? ? int age=getAge(5);
printf("第5個人的年齡是%d歲", age);?
return 0;
}
2019-01-03
n 少了遞推呀,遞推函數(shù)中應(yīng)該是 n-1
2018-12-30
#include <stdio.h>?
int age(int n)
{
? ? int Age;
? ? if(n==1)
? ? {
? ? Age=10;? ? ?
? ? }
? ? else
? ? {
? ? ? ? Age=age(n-1)+2;
? ? }
? ? return Age;
}
int main()?
{
? ? int fiveage=age(5);
printf("第5個人的年齡是%d歲",fiveage);?
return 0;
}