求助!!

P1015 [NOIP1999 普及组] 回文数

yiyezhiqiu1011 @ 2024-04-08 13:47:27

#include <bits/stdc++.h>
using namespace std;
int m,cnt;
string number;

bool hui(string s)
{
    int l = s.size();
    for (int i = 0; i < l/2; i++)
        if (s[i] != s[l - i - 1])
            return false;
    return true;
}

string pl(string n)
{
    int l = n.size(),in = 0;
    string nn = n;
    for (int i = l-1; i >= 0; i--)
    {
        n[i] = (char)((int)n[i] + nn[l - i - 1] + '0' + in);
        in = (n[i] - '0') / m;
        n[i] = (n[i] - '0') % m + '0';
    }
    if (in > 0)
    {
        n = "1" + n;
    }

    return n;
}

void d(string n)
{
    cout << n << endl;
    if (hui(n) || cnt > 30)
        return;

    ++cnt; 
    d(pl(n));
}

int main()
{
    cin >> number;
    cin >> m;

    d(number);

    if (cnt <= 30)
        cout << "STEP=" << cnt;
    else
        cout << "Impossible!";
}

|