实现n的阶乘
源代码:
#include<stdio.h>
#include<conio.h>
int fact(int n);
int main(){
int i;
printf("请输入要求的数的阶乘: ");
scanf("d%",&i);
printf("%d的阶乘结果为:%d\n",i,fact(i));
getch();
return 0;
}
int fact(int n){
if(n==1)
return 1;
else
return n*fact(n-1);
}
通过此算法,vs2010报错,堆栈溢出,对于这类问题不是简单的直接改变堆栈内存尺寸就可以,需要优化递归的算法。比如:可以设置一个动态的空间,可以free空间。参照点击打开链接的使用方法进行改进。