c++ 最后一个数据点报错,但验算50的数据是正确的,不知道哪里出错了

P1009 [NOIP1998 普及组] 阶乘之和

brave_heart @ 2022-10-01 17:17:10

#include <iostream>
#include <numeric>

using namespace std;

int main() {
    int a[101] = {};
    int b[101] = {0,1};
    int n;
    cin >> n;
    int count = 1,cx = 0,aount=1,ax=0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= count; j++) {
            int k = b[j];
            b[j] = (cx + k * i) % 10;
            cx = (cx + k * i) / 10;
            if (j == count && cx != 0)
                count++;
        }
        for (int j = 1; j <= aount; j++) {
            int k = a[j];
            a[j] = (k + b[j] + ax)%10;
            ax = (k + b[j] + ax) / 10;
            if (j == aount && (ax != 0 || b[j + 1] != 0))
                aount++;
        }
    }
    for (int i = count; i >= 1; i--) {
        cout << b[i];
    }
    cout << endl;
    for (int i = aount; i >= 1; i--) {
        cout << a[i];
    }

    return 0;
}

就是使用数组先去计算阶乘的数据,然后加到累加数组上面,其中设置了进位。 前三个数据都通过了,最后一个报错了。


by 12qwaszxQAZ @ 2022-10-01 17:27:57

不知道


by dingshengyang @ 2022-10-01 17:44:07

@brave_heart 什么叫报错?WA or others?


by brave_heart @ 2022-10-01 22:25:55

@dingshengyang 抱歉没说明白,就是 WA


by brave_heart @ 2022-10-01 22:27:55

代码输出部分这里我是忘了修改了,正常是没有

for(int i = count; i >= 1; i--){
    cout<<b[i];
}

这个部分的


by dingshengyang @ 2022-10-02 11:27:29

@brave_heart AC了吗


by brave_heart @ 2022-10-03 22:24:37

@dingshengyang 还是没有,找不到哪里的错误,我用了高位计算器核对到了40阶乘和都没找到


by dingshengyang @ 2022-10-04 09:15:10

@brave_heart 我看看


|