- //使用循环来求阶乘
- #include <stdio.h>
- long fact(int);
- int main()
- {
- /*long s, i;
- for(i=1, s=1; i<=4; i++)
- {
- s=s*i;
- }
- printf("%d", s);*/ //普通的for循环阶乘
- printf("%d", fact(4));
- return 0;
- }
- /*
- 4!=1*2*3*4
- 3!=1*2*3
- 2!=1*2
- 1!=1
- 递归的思路
- */
- long fact(int n)
- {
- long f;
- //printf("%d\\n", n);建议把下面三行注释去掉,运行试试,发现其规律
- if(n>1)
- {
- f =n * fact(n-1);
- //printf("%d\\n", n);
- }
- else
- {
- f=1;
- //printf("%d\\n", n);
- }
- return f;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0206201512735.html
来源: http://www.codesnippet.cn/detail/0206201512735.html