zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    2491 玉蟾宫
    1704 卡片游戏
    1020 孪生蜘蛛
    1215 迷宫
    3149 爱改名的小融 2
    1316 文化之旅 2012年NOIP全国联赛普及组
    1664 清凉冷水
    157. [USACO Nov07] 奶牛跨栏
    [SCOI2005]繁忙的都市
    【NOIP2014模拟赛No.1】我要的幸福
  • 原文地址:https://www.cnblogs.com/dreamzj/p/13894003.html
Copyright © 2011-2022 走看看