版权声明:©2018-2020 Macromedia, Inc. All rights reserved. 文章为时间的荒野所原创如需转载请联系博主。 https://blog.csdn.net/qq_43444398/article/details/89007116
本题要求实现一个计算非负整数阶乘的简单函数。
函数接口定义:
int Factorial( const int N );
其中N是用户传入的参数,其值不超过12。如果N是非负整数,则该函数必须返回N的阶乘,否则返回0。
裁判测试程序样例:
#include <stdio.h>
int Factorial( const int N );
int main()
{
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF) printf("%d! = %d\n", N, NF);
else printf("Invalid input\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
5
输出样例:
5! = 120
这个题花了我一点时间,想想就想笑,0的阶乘实际上为1,我一直以为是1。
int Factorial( const int N )
{
if(N<0)
{
return 0;
}
else if(N==0)
{
}
int sum=1;
for(int i=1;i<=N;i++)
{
sum*=i;
}
return sum;
}