码迷,mamicode.com
首页 > 其他好文 > 详细

大数运算 N!

时间:2020-05-02 12:16:52      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:ace   namespace   com   return   show   inf   scanf   src   cstring   

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");
    }
}

 

大数运算 N!

标签:ace   namespace   com   return   show   inf   scanf   src   cstring   

原文地址:https://www.cnblogs.com/Jason66661010/p/12817258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!