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 spa

輸出樣例:

5! = 120code

 

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接口

相關文章
相關標籤/搜索