其他分享
首页 > 其他分享> > N的阶乘

N的阶乘

作者:互联网

\(0 \le N \le 1000\),求\(N!\),考察高精度乘法。

const int N=3010;
struct bignum
{
    int m[N];
    int len;
    bignum()
    {
        memset(m,0,sizeof m);
        len=0;
    }
};
int n;

bignum mul(bignum a,int b)
{
    bignum c;
    c.len=a.len;

    int carry=0;
    for(int i=0;i<c.len;i++)
    {
        int t=a.m[i]*b+carry;
        c.m[i]=t%10;
        carry=t/10;
    }

    while(carry)
    {
        c.m[c.len++]=carry%10;
        carry/=10;
    }
    return c;
}

int main()
{
    while(cin>>n)
    {
        bignum a;
        a.m[a.len++]=1;

        for(int i=1;i<=n;i++) a=mul(a,i);

        for(int i=a.len-1;i>=0;i--) cout<<a.m[i];
        cout<<endl;
    }
    //system("pause");
    return 0;
}

标签:le,bignum,int,len,--,阶乘
来源: https://www.cnblogs.com/fxh0707/p/14403788.html