c语言阶乘的非递归函数 c语言阶乘递归代码

用c语言的递归和非递归方法求一个数的阶乘问题#include stdio.h
int rf(int n)
{
return n0 ? n * rf(n-1) : 1;
}
int f(int n)
{
int k = 1;
while(n0)
k *= n--;
return k;
}
int main()
{
int n;
scanf("%d", n);
printf("递归:%d\n", rf(n));
printf("非递归:%d\n", f(n));
}
编写递归和非递归求阶乘的函数,用C语言C语言递归函数和非递归函数求阶乘,参考代码如下:
#includestdio.h
long fun1(int n)
{
if(n=1) return 1;
return fun1(n-1)*n;
}
long fun2(int n)
{
int i;
long m=1;
for(i=1; i=n;i)
m*=i;
return m;
}
int main()
{
printf("%ld\n",fun1(9));
printf("%ld\n",fun2(9));
return 0;
}
用数据结构(C语言)中的栈实现阶乘,不是用递归,请问要怎么写?。浚≒S:求能运行出来的代码)int stack[STACK_SIZE];
int *pStackTop=stack[0];
bool stackFull,stackEmpty=true;
int* pop()
{
int *ret=NULL;
if(stackEmpty)
retur NULL;
else
{
if(pStackTop==stack[0])
{
ret=pStackTop;
stackEmpty=true;
}
else
{
ret=pStackTop--;
}
}
return ret;
}
void push(int val)
{
if(stackFull)
return;
if(stackEmpty)
{
*pStackTop=val;
stackEmpty=false;
}
else
{
*pStackTop=val;
}
if(pStackTop==stack[STACK_SIZE-1])
{
stackFull=true;
}
【c语言阶乘的非递归函数 c语言阶乘递归代码】}
int main(int n)
{
int sum=1;
push(1);
if(n==1)
{
goto end;
}
for(int i=2;i=n;i)
{
int *s=pop();
push(i*(*s));
}
end:
return *pop();
}
c语言阶乘的非递归函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于c语言阶乘递归代码、c语言阶乘的非递归函数的信息别忘了在本站进行查找喔 。

    推荐阅读