14323 Factorial

链接:https://ac.nowcoder.com/acm/problem/14323
来源:牛客网

题目描述

Hill was a clever boy,he like math very much.Today teacher give him a question.
calculate N! . But Hill was tired,he need to sleep,so let you help him to calculate N!.

what is N!
N! = 1*2*3*......*N

Hey, you need to think about 0! . Do you?

输入描述:

There are multiple test cases. The first line is an positive integer T indicating the number of test cases.(0<T<=1000)
For each test case:
A positive integer N(0<=N<=20)

输出描述:

For each test case, output one line.
示例1

输入

3
1
2
3

输出

1
2
6
题意:
求T组N的阶乘

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<cmath>
using namespace std;
const int maxn=10020;
long long sum[maxn];
long long dfs(int n){
    int i;
    if(n==0){
        return 1;
    }
    else if(n==1){
        
        return 1;
    }
    else if(sum[n]!=0){
        return sum[n];
    }
    else{
        for(int i=2;i<=n;i++)
        sum[i]=sum[i-1]*i;
        return sum[n];
    }
}

int main(){
    int T,N;
    sum[1]=1;
    long long m;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        m=dfs(N);
        printf("%lld
",m);
    } 
    
    return 0;
}
原文地址:https://www.cnblogs.com/dreamzj/p/13894003.html