Problem E: 深入淺出學算法019-求n的階乘
Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 5077 Solved: 3148
Description
求階乘,采用遞歸的方法,你會寫嗎?
Input
多組測試數據,首先輸入整數T表示組數 然后每一組在一行輸入一個整數n( 1 <= n <= 10)
Output
對於每組數據輸出一行,值為n的階乘
Sample Input
1 2
Sample Output
2
HINT
使用遞歸函數求n!
int fact(int n)
{
}
#include<stdio.h> int fact(int n) { int result; if(n==1||n==0) result=1; else result=n*fact(n-1); return result; } int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",fact(n)); } return 0; }