gpt4 book ai didi

c - C 中的递归阶乘程序困惑

转载 作者:行者123 更新时间:2023-11-30 21:30:12 25 4
gpt4 key购买 nike

int factorial(int n); 
int main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

请解释一下这个程序是如何工作的。我在阶乘函数的 if 语句之后应用了 for 循环,但这是如何工作的。

最佳答案

int factorial(int n){
if(n==1)
return 1;
return n*(factorial(n-1));

}

这是正确的:)我认为你的代码不应该编译,因为函数 Factorial(int n) 没有定义的返回语句!

关于c - C 中的递归阶乘程序困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com