大数运算 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");
    }
}

 

posted @ 2020-05-02 10:41  Jason66661010  阅读(144)  评论(0编辑  收藏  举报