本题要求实现一个计算非负整数阶乘的简单函数。函数
时间限制: 400ms内存限制: 64MB代码长度限制: 16KB
int Factorial( const int N );
其中N
是用户传入的参数,其值不超过12。若是N
是非负整数,则该函数必须返回N
的阶乘,不然返回0。测试
1 #include <stdio.h> 2 int Factorial(const int N); 3 int main() 4 { 5 int N, NF; 6 scanf_s("%d", &N); 7 NF = Factorial(N); 8 if (NF) 9 printf_s("%d! = %d\n", N, NF); 10 else 11 printf_s("Invalid input\n"); 12 return 0; 13 } 14 /* 你的代码将被嵌在这里 */
5
spa
5! = 120
code
1 int Factorial(const int N) 2 { 3 if (N < 0) 4 return 0; 5 if (N == 0) 6 return 1; 7 else 8 return N * Factorial(N - 1); 9 }
做者:耑新新,发布于 博客园blog
转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmail.com接口