2022-09-05

P1009 [NOIP1998 普及组] 阶乘之和

Tom_catLLL @ 2022-11-03 17:11:25

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;

int main()
{
    int n, a[1001] = { 0 }, b[1001] = { 0 },i,j;
    cin >> n;
    a[0] = b[0] = 1;
    for (i = 2; i <= n; i++)
    {
        for (j = 0; j < 100; j++)
            a[j] *= i;
        for (j = 0; j < 100; j++)
        {
            if (a[j] > 9)
            {
                a[j + 1] += a[j] / 10;
                a[j] %= 10;
            }
        }
        for (j= 0; j < 100; j++)
        {
            b[j] += a[j];
            if (b[j] > 9)
            {
                b[j + 1] = b[j] / 10;
                b[j] %= 10;
            }
        }
    }
    for (i=100; i>=0 && b[i]==0;i--);
    for (j=i; j >= 0; j--)
        cout << b[j];
    system("pause");
    return 0;
}

by Tom_catLLL @ 2022-11-08 16:05:52

@xiaohaoaibiancheng66 你是故意找茬是吧,就问你看不看吧(狗头)


by xiaohaoaibiancheng66 @ 2022-11-08 17:37:30

@Tom_catLLL 如果加上这句可能就会TLE


by Tom_catLLL @ 2022-11-09 09:17:58

@xiaohaoaibiancheng66 没有,我找到问题了。在第一个for大循环里面的第三个循环里的判断语句,b【j+1】=b[j]/10;应该改成b【j+1】+=b【j】/10;


上一页 |