zoukankan      html  css  js  c++  java
  • 2006年清华:N的阶乘

    题目描述:

     输入一个正整数N,输出N的阶乘。

    输入:

    正整数N(0<=N<=1000)

    输出:

     输入可能包括多组数据,对于每一组输入数据,输出N的阶乘

    样例输入:
    4
    5
    15
    样例输出:
    24
    120
    1307674368000

    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAXN=10005;
    struct BigInt{
        int e[MAXN];
        int len;
        BigInt()
        {
            memset(e,0,sizeof(e));
            len=0;
        }
        BigInt(long long v)
        {
            memset(e,0,sizeof(e));
            len=0;
            while(v!=0)
            {
                int k=v%10;
                e[len++]=k;
                v/=10;
            }
        }
        BigInt operator*(const BigInt &t)const
        {
            BigInt res;
            for(int i=0;i<len;i++)
            {
                int up=0;
                for(int j=0;j<t.len;j++)
                {
                    int z=e[i]*t.e[j]+up+res.e[i+j];
                    res.e[i+j]=z%10;
                    up=z/10;
                }
                if(up!=0)
                {
                    res.e[i+t.len]=up;
                }
            }
            res.len=len+t.len;
            while(res.len>1&&res.e[res.len-1]==0)    res.len--;
            return res;
        }
        void print()
        {
            for(int i=len-1;i>=0;i--)
                printf("%d",e[i]);
            printf("
    ");
        }
    }res[1005];
    int main()
    {
        int n;
        res[0].e[0]=1;
        res[0].len=1;
        for(int i=1;i<=1000;i++)
        {
            BigInt t(i);
            res[i]=res[i-1]*t;
        }
        while(scanf("%d",&n)!=EOF)
        {
            res[n].print();
        }
        return 0;
    }
  • 相关阅读:
    (判断是否为弱联通分量) poj 2762
    (最大生成树) poj 1979
    (暴力) bzoj 2208
    (BFS) bzoj 1102
    (并查集) bzoj 1161
    (数学) bzoj 1800
    (博弈) bzoj 2460
    (dinic) poj 3469
    (双端队列优化的SPFA) bzoj 2100
    (判断负环) bzoj 2019
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5472379.html
Copyright © 2011-2022 走看看