zoukankan      html  css  js  c++  java
  • 大数运算 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("
    ");
        }
    }

    有结构体:

    #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("
    ");
        }
    }
  • 相关阅读:
    numpy 矩阵和数组
    python map()
    python matplotlib plot
    python mean()
    预测数值型数据:回归
    散点图
    非均衡分类问题
    AdaBoost元算法
    2.1 n元排列
    1.3 数域
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12817258.html
Copyright © 2011-2022 走看看