热门

最新

红包

立Flag

投票

同城

我的

发布
weixin_51736184
芒果加奶
5 年前
trueweixin_51736184

/*

试题描述
本题要求实现一个计算非负整数阶乘的简单函数。

函数接口定义:

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, NF);
else printf("Invalid input");

return 0;
}
输入
非负整数N
输出
如果N是非负整数,则该函数必须返回N的阶乘,否则返回Invalid input
样例输入
5
样例输出
5! = 120
*/


#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, NF);
else printf("Invalid input");

return 0;
}

int Factorial(const int N){
int sum=1,i;
if(N<0){
return 0;
}
else{
for(i=1;i<=N;i++){
sum=sum*i;
}
return sum;
}
}

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
2021.9.15华为笔试直接难哭,少女爆哭这已经是第二次华为笔试了这么难估计是不想招人了菜狗
立即登录