【发布时间】:2021-11-16 13:06:15
【问题描述】:
#include <stdio.h>
int factorial(int n);
void main()
{
int n;
printf("Enter your number : " );
scanf("%d",&n);
if(n <= 1)
{
printf("The factorial of the number n is ",n);
}
else
{
int res = factorial(n);
printf("The result is %d\n",res);
}
}
int factorial(int n)
{
if(n <= 1)
return 1;
return n * factorial(n-1);
}
我第一次做一个递归函数的概念,我对递归的概念几乎掌握了 65%。在上面的程序中,我编写了一个阶乘递归函数,它运行正常,我得到了输出,但我试图思考递归在哪里结束
例如,我输入了 5:
The result is 120
但我想要的主要是为什么它在 0 之后不继续,如果 n
【问题讨论】:
-
65% - 你是怎么得出这个数字的?只是好奇,:)
-
@SouravGhosh 73% 的统计数据是现场制作的。
-
当
n <= 1时,您只返回1,而不是1 * factorial(n - 1)。由于此处未调用factorial,因此不会发生n = 0, -1, ... -
@VLAZ 有 87% 的错误概率,而 67% 的人在 91% 的时间内都这样做了。
-
@Sourav Ghosh 我收到了号码