Attention: 如果喜歡我寫的文章懊悯,歡迎來我的github主頁給star
Github:github.com/MuziJin
本題要求實現(xiàn)一個計算非負(fù)整數(shù)階乘的簡單函數(shù)载城。
函數(shù)接口定義:
int Factorial( const int N );
其中N是用戶傳入的參數(shù)脱衙,其值不超過12。如果N是非負(fù)整數(shù)划乖,則該函數(shù)必須返回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
Code
int Factorial( const int N ) //遞歸思想基公,注意邊界條件
{
int temp = 0;
if ( N>=0)
{
if( N==1 || N==0) temp = 1;
else temp = N * Factorial(N-1);
}
return temp;
}
轉(zhuǎn)載請注明出處:github.com/MuziJin