PTA之简单阶乘计算

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

时间限制: 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

输出样例:

5! = 120

 

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 }

 

作者:耑新新,发布于  博客园

转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmail.com

转载于:https://www.cnblogs.com/Amedeo/p/9079210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值