您的位置:首页 > 其它

4-8 简单阶乘计算

2016-02-19 15:57 169 查看
本题要求实现一个计算非负整数阶乘的简单函数。


函数接口定义:

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



int Factorial( const int N )
{
if (N >= 0)
{
int fact = 1;
for (int i = 2; i <= N; i++)
fact *= i;
return fact;
}
else
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: