Problem E: 深入浅出学算法019-求n的阶乘


Problem E: 深入浅出学算法019-求n的阶乘

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM