其他分享
首页 > 其他分享> > 大数运算 N!

大数运算 N!

作者:互联网

http://acm.hdu.edu.cn/showproblem.php?pid=1042

 

 代码

无结构体:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int aa[40000];

int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        aa[0] = 1;
        int len = 1;
        for (int i = 1; i <= n; i++)
        {
            int t = 0;
            for (int j = 1; j <=len; j++)
            {
                t = aa[j-1] * i + t;
                aa[j-1] = t % 10;
                t /= 10;
            }
            while (t != 0)
            {
                aa[len++] = t % 10;
                t /= 10;
            }
        
        }
        for (int i =len - 1; i >= 0; i--)
            printf("%d", aa[i]);
        printf("\n");
    }
}

有结构体:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct bignum
{
    int list[40000];
    int len;
    bignum()
    {
        memset(list, 0, sizeof(list));
        len = 0;
    }
}aa;
bignum mul2(bignum a, int b)
{
    bignum c;
    int t = 0, i;
    for (i = 0; i < a.len; i++)
    {
        t = a.list[i] * b + t;
        c.list[c.len++] = t % 10;
        t /= 10;
    }
    while (t != 0)
    {
        c.list[c.len++] = t % 10;
        t /= 10;
    }
    return c;
}
int main()
{
    int n;
    while (scanf("%d", &n) != EOF)
    {
        aa.len = 1; aa.list[0] = 1;
        for (int i = 1; i <= n; i++)
            aa = mul2(aa, i);
        for (int i = aa.len - 1; i >= 0; i--)
            printf("%d", aa.list[i]);
        printf("\n");
    }
}

 

标签:aa,bignum,运算,大数,int,list,len,include
来源: https://www.cnblogs.com/Jason66661010/p/12817258.html