关于递归阶乘问题 答案老是0,求大神指点
//运用递归计算出n以内所以数的阶乘之和# include <stdio.h>
int f(int n);
int sum(double n);
int main (void)
{
double n;
printf("Please input you want 阶乘 number: ");
scanf("%lf", &n);
printf("sum = %lf\n", sum(n));
return 0;
}
//n的阶乘
int f(double n)
{
if(n == 0 || n==1)
{
return 1;
}
else
{
return n*f(n-1); //运用递归的原理,不断调用自身
}
}
//遍历n以内的每个数,把每个数的阶乘相加
int sum(double n)
{
double i;
double sum;
for(i=1; i<=n; i++)
sum += f(i);
return sum;
}
[此贴子已经被作者于2016-4-20 11:54编辑过]