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;
}
上一篇:洛谷P1932 A+B A-B A*B A/B A%B Problem 高精类


下一篇:C# base 与 this