第三个WA了,其他AC

P1015 [NOIP1999 普及组] 回文数

liumingdi @ 2024-10-27 21:46:23

神犇看一下怎么回事

源代码

//#include <bits/stdc++.h>
#include <algorithm>
#include <string>
#include<iostream>
using namespace std;

bool is_huiwen(const string &str) {
    int len = str.length();
    for (int i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            return false;
        }
    }
    return true;
}

string reverse_str(string str) {
    reverse(str.begin(), str.end());
    return str;
}

unsigned int string2num(const string str, int scale) {
    unsigned int result = 0;
    for (int i = 0; i < str.length(); i++) {
        result *= scale;
        if (str[i] >= '0' && str[i] <= '9')
            result += str[i] - '0';
        else {
            result += str[i] - 'A' + 10;
        }
    }
    return result;
}

string num2string(unsigned int num, int scale) {
    string str = "";
    while (num) {
        int rem = num % scale;
        num /= scale;
        if (rem >= 0 && rem <= 9) {
            str += rem + '0';
        }
        else {
            str += rem + 'A' - 10;
        }
    }
    return reverse_str(str);
}

string plus_str(const string &num1, const string &num2, int scale) {
    //cout << "num1: " << string2num(num1, scale) << ", num2: " << string2num(num2, scale) << endl;
    unsigned int num = string2num(num1, scale) + string2num(num2, scale);
    return num2string(num, scale);
}

int main() {
    int scale, step = 0;
    string number;
    cin >> scale >> number;
    //string str = num2string(number, scale);
    string str = number;
    while (step <= 30) {
        step++;
        str = plus_str(str, reverse_str(str), scale);
        //cout << str << endl;
        if (is_huiwen(str)) {
            cout << "STEP=" << step;
            break;
        }
    }
    if (step > 30) {
        cout << "Impossible!";
    }
}

第三个WA了,其他AC

帮忙看一眼哪里错了


|