C++求助

P1009 [NOIP1998 普及组] 阶乘之和

zcy6666666 @ 2023-10-27 22:53:06

这个代码为什么会WA,下载了测试数据是没有问题的。

#include<bits/stdc++.h>
using namespace std;
int n, a[101] = { 0 }, s[101] = { 0 };

void want1(int x)
{
    int m = 0;
    for (int i = 100; i >= 0; i--)
    {
        a[i] *= x + m;
        m = a[i] / 10;
        a[i] %= 10;
    }
}

void want2()
{
    int m = 0;
    for (int i = 100; i >= 0; i--)
    {
        s[i] += a[i] + m;
        m = s[i] / 10;
        s[i] %= 10;
    }
}

int main()
{
    cin >> n;
    a[100] = 1;
    for (int i = 1; i <= n; i++)
    {
        want1(i);
        want2();
    }
    int w;
    for (int i = 0; i <= 100; i++)
    {
        if (a[i] > 0)
        {
            w = i;
            break;
        }
    }
    for (int i = w; i <= 100; i++)
        cout << s[i];
}

by NoBDKnowsBeterThanME @ 2023-10-27 23:32:36

第十行有问题
应该是(a[i] *= x) += m;


by zcy6666666 @ 2023-10-28 01:26:39

@NoBDKnowsBeterThanME是的是的,犯这种低级错误,谢谢


|