代码未通过,求帮助

P1015 [NOIP1999 普及组] 回文数

zhaokp @ 2024-01-20 22:28:42

本地测试样例通过,但是测试点第三个AC其他WA。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int N, M[131], W[131], S[131], len;
int num;
string s1;
inline void init(){
    //s1->m
    for (int i=0; i<len; ++i){
        num = s1[len-1-i]-'0';
        num = num >= 49 ? num - 39 : num;
        M[i] = num;
    }
}

inline bool reverseAndAdd(int *m, int *w, int *s){
    //reverse(m)->w
    for(int i=0; i<len; ++i){
        w[i]=m[len-1-i];
    }
    //m+w->s
    bool c=false;
    for(int i=0; i<len; ++i){
        s[i]=m[i]+w[i]+c;
        c=false;
        if(s[i] >= N){
            s[i] %= N;
            c=true;
        }
    }
    if(c){
        s[len]=1;
        ++len;
    }
    //check
    for(int i=0; i<=len/2+1; ++i){
        if(!(s[i]==s[len-1-i])) return false;
    }
    return true;
}

int main(){
    scanf("%lld", &N);
    cin>>s1;
    len=s1.length();
    init();
    int *m=M;
    int *w=W;
    int *s=S;
    for(int i=0; i<len/2+1; ++i){
        if(!(m[i]==m[len-1-i])) break;
        cout<<"STEP=0";
        return 0;
    }
    for(int j=1;j<=30;++j){
        if(reverseAndAdd(m,w,s)){
            cout<<"STEP="<<j;
            return 0;
        }
        //s->m,m->s
        int* temp = s;
        s = m;
        m = temp;
    }
    cout<<"Impossible!";

    return 0;
}

by sbno333 @ 2024-01-20 23:13:20

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int N, M[131], W[131], S[131], len;
int num;
string s1;
inline void init(){
    //s1->m
    for (int i=0; i<len; ++i){
        num = s1[len-1-i]-'0';
        num = num >= 10 ? num + '0' - 'A' + 10: num;//注意已经减过
        M[i] = num;
    }
}

inline bool reverseAndAdd(int *m, int *w, int *s){
    //reverse(m)->w
    for(int i=0; i<len; ++i){
        w[i]=m[len-1-i];
    }
    //m+w->s
    bool c=false;
    for(int i=0; i<len; ++i){
        s[i]=m[i]+w[i]+c;
        c=false;
        if(s[i] >= N){
            s[i] %= N;
            c=true;
        }
    }
    if(c){
        s[len]=1;
        ++len;
    }
    //check
    for(int i=0; i<=len/2+1; ++i){
        if(!(s[i]==s[len-1-i])) return false;
    }
    return true;
}

int main(){
    scanf("%lld", &N);
    cin>>s1;
    len=s1.length();
    init();
    int *m=M;
    int *w=W;
    int *s=S;
    bool kkk;
    kkk=1;
    for(int i=0; i<len/2+1; ++i){
        if(!(m[i]==m[len-1-i])){
            kkk=0;
            break;
        } 

    }
    if(kkk){//循环自己注意点,逻辑严谨一些
        cout<<"STEP=0";
        return 0;
    }

    for(int j=1;j<=30;++j){
        if(reverseAndAdd(m,w,s)){
            cout<<"STEP="<<j;
            return 0;
        }
        //s->m,m->s
        int* temp = s;
        s = m;
        m = temp;
    }
    cout<<"Impossible!";

    return 0;
}

建议自己下载数据调试


|