样例过了,自己输入也没问题,但是0分?

P1591 阶乘数码

letusgo @ 2023-11-17 21:51:15

#include<bits/stdc++.h>
using namespace std;
int a[1000],len;
void fac(int n){
    int carry = 0;
    for(int i = 1; i <= len; i++){
        a[i] = a[i]*n + carry;
        carry = a[i]/10;
        a[i] %= 10;
    }
    while(carry > 0){
        a[++len] = carry%10;
        carry /= 10;
    }
}
int main(){
    int t,n,a1;
    cin >> t;
    for(int k = 1; k <= t; k++){
        int cnt = 0;
        len = 1;
        memset(a,0,sizeof(a));
        a[1] = 1;
        cin >> n >> a1;
        for(int i = 1; i <= n; i++) fac(i);
        for(int i = len; i >= 1; i--){
            if(a1 == a[i]) cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

by sfqxx1 @ 2023-11-17 22:00:27

代码逻辑是没有问题的,一般这种情况开 O2 就能解决(原理我不知道),发现你数组开小了,修改代码:

#include<bits/stdc++.h>
using namespace std;
int a[100000],len;
void fac(int n){
    int carry = 0;
    for(int i = 1; i <= len; i++){
        a[i] = a[i]*n + carry;
        carry = a[i]/10;
        a[i] %= 10;
    }
    while(carry > 0){
        a[++len] = carry%10;
        carry /= 10;
    }
}
int main(){
    int t,n,a1;
    cin >> t;
    for(int k = 1; k <= t; k++){
        int cnt = 0;
        len = 1;
        memset(a,0,sizeof(a));
        a[1] = 1;
        cin >> n >> a1;
        for(int i = 1; i <= n; i++) fac(i);
        for(int i = len; i >= 1; i--){
            if(a1 == a[i]) cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

by sfqxx1 @ 2023-11-17 22:01:02

@zhouzihang1 开 O2 过了


by zhouzihang1 @ 2023-11-17 22:02:05

哦,抱歉,没仔细看代码,以为写的int


by letusgo @ 2023-11-17 23:31:27

@sfqxx1 感谢感谢


by DaD114514 @ 2024-01-12 08:45:37

我这更神奇,我关了O2结果0分变全AC了?


|