Java教程

P1009 [NOIP1998 普及组] 阶乘之和

本文主要是介绍P1009 [NOIP1998 普及组] 阶乘之和,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目链接

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

题目思路

本题为高精度乘法 + 高精度加法

题目代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
const int N = 1010;
int n;
int a[N], b[N];


int main()
{
    cin >> n;
    a[0] = b[0] = 1;
    for(int i = 2; i <= n; i ++ )
    {
        for(int j = 0; j < 100; j ++ )
            b[j] *= i;
        for(int j = 0; j < 100; j ++ )
            if(b[j] > 9)
            {
                b[j + 1] += b[j] / 10;
                b[j] %= 10;
            }
        
        for(int j = 0; j < 100; j ++ )
        {
            a[j] += b[j];
            if(a[j] > 9)
            {
                a[j + 1] += a[j] / 10;
                a[j] %= 10;
            }
        }
    }
    
    int res;
    for(res = 100; res >= 0 && a[res] == 0; res -- );
    for(int j = res; j >= 0; j -- ) cout << a[j];
     
    return 0;
}
这篇关于P1009 [NOIP1998 普及组] 阶乘之和的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!