各位神犇帮帮这位蒟蒻吧

P1015 [NOIP1999 普及组] 回文数

bycwxbd @ 2023-11-27 17:24:51

#include <bits/stdc++.h>

using namespace std;
namespace work{
    const int N = 300;
    int a[N], b[N], c[N], c1, c2, c3, n, ans;
string s1;

void add(){
    for(int i = s1.size() - 1; i >= 0; i--){
        if(s1[i] >= '0' && s1[i] <= '9'){
            a[++c1] = s1[i] - '0';
        }else{
            a[++c1] = s1[i] - 'A' + 10;
        }
    }
}

void turn(){
    c2 = 0;
    for(int i = c1; i >= 1; i--){
        b[++c2] = a[i];
    }
}

void plus(){
    c3 = max(c1, c2) + 2;
    memset(c, 0, sizeof c);
    for(int i = 1; i <= c3; i++){
        c[i] += a[i] + b[i];
        c[i + 1] = c[i] / n;
        c[i] %= n;
    }
    while(c[c3] == 0){
        c3--;
    }
}

bool check(){
    for(int i = 1; i <= c3; i++){
        if(c[i] != c[c3 - i + 1]) return 0;
    }
    return 1;
}

void is(){
    c1 = c3;
    for(int i = 1; i <= c3; i++){
        a[i] = c[i]; 
    }
}

void print(){
    cout << "a=";
    for(int i = 1; i <= c1; i++){
        cout << a[i];
    }
    cout << endl;
    cout << "b=";
    for(int i = 1; i <= c2; i++){
        cout << b[i];
    }
    cout << endl;
    cout << "c="; 
    for(int i = 1; i <= c3; i++){
        cout << c[i];
    }
    cout << endl;
}

int main(){
    cin >> n >> s1;
    add();
    turn();
    //print();
    plus();
    //print();
    ans = 1;
    while(!check()){
        is();
        turn();
        plus();
        //print();
        ans++;
    }
    if(ans <= 30){
        cout << "STEP=" << ans << endl;
    }else{
        cout << "Impossible!" << endl;
    }
    return 0;
}
}

int main(){
    work::main();
    return 0;
}

前3个点AC,最后一个RE


by ilibilib @ 2023-11-27 17:33:57

@bycwxbd 不开O2就过了


by bycwxbd @ 2023-11-27 17:37:31

@ilibilib 谢谢神犇!!!


|