本題要求實現一個計算非負整數階乘的簡單函數。函數
時間限制: 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! = 120
code
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接口