zoukankan      html  css  js  c++  java
  • 模拟与高精度 P1009 阶乘之和

    题目

    https://www.luogu.com.cn/problem/P1009

    代码

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    using namespace std;
    #define max 2010
    
    vector<int> mul(vector<int> a, int b)
    {
        vector<int>p;
        int t = 0;
        for (int i = 0; i < a.size(); i++)
        {
            p.push_back((t + a[i] * b) % 10);
            t = (t + a[i] * b) / 10;
        }
        while (t != 0)
        {
            p.push_back(t % 10);
            t /= 10;
        }
        return p;
    }
    vector<int>add(vector<int>a, vector<int>b)
    {
        vector<int>p,tmpa,tmpb;
        tmpa = a; tmpb = b;
        int aa = tmpa.size(), bb = tmpb.size();
        if (aa > bb)
            for (int i = 0; i < aa - bb;i++)tmpb.push_back(0);
        if (aa < bb)
            for (int i = 0; i < bb-aa; i++)tmpa.push_back(0);
        int t = 0;
        for (int i = 0; i < tmpa.size(); i++)
        {
            p.push_back((t + tmpa[i] + tmpb[i]) % 10);
            t = (t + tmpa[i] + tmpb[i]) / 10;
        }
        if (t != 0)
            p.push_back(t);
        return p;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        std::cin.tie(0);
        std::cout.tie(0);
        int t;
        cin >> t;
        vector<int>p,answer;
        p.push_back(1);
        answer.push_back(1);
        for (int i = 2; i <= t; i++)
        {
            p = mul(p, i);
            answer = add(answer, p);
        }
        bool ok = false;
        for (int i = answer.size() - 1; i >= 0; i--)
        {
            if (answer[i] != 0)ok = true;
            if (ok)cout << answer[i];
        }
        if (!ok)cout << 0;
    }
  • 相关阅读:
    汉语-汉字:鬲
    汉语-汉字:鬻
    汉语-汉字:軎
    汉语-汉字:辔
    汉语-汉字:燮
    汉语-汉字:夔
    汉语-汉字:鬯、畅
    4-2电子时钟中的运算符重载
    华为OJ:计算字符个数
    POJ 3071 Football 【概率DP】
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12839070.html
Copyright © 2011-2022 走看看