求助,三个re,一个wa

P1015 [NOIP1999 普及组] 回文数

yiyezhiqiu1011 @ 2023-07-25 14:48:51

#include<bits/stdc++.h>
using namespace std;
int alen,blen,n;
bool hui(int h[])
{
    for (int i = 0; i < alen; i++)
    {
        if (h[i] != h[alen - i - 1])
            return false;
    }
    return true;
}

int wei(int n1)
{
    int cnt = 0;
    while (n1)
    {
        cnt++;
        n1 /= 10;
    }
    return cnt;
}

void jia(int a[],int b[])
{
    int in = 0;
    for (int i = 0; i < alen; i++)
    {
        a[i] += a[i] + in + b[i];
        in = a[i] / n;
        a[i] %= n;
    }
    if (in)
    {
        a[alen] = in;
        alen++;
        blen++;
    }
    for (int i = 0; i < blen; i++)
        b[blen-1-i] = a[i];
}

int main()
{
    int a[300]={},b[300]={},m;
    cin >> n >> m;
    alen = wei(m);
    blen = alen;
    for (int i = wei(m)-1; i >= 0; i--)
    {
        a[i] = n % 10;
        b[blen-1-i] = a[i];
        n /= 10;
    }

    for (int i = 1; i <= 30; i++)
    {
        jia(a,b);
        if (hui(a))
        {
            cout << "STEP=" << i; 
            return 0;
        }
    }

    cout << "Impossible!";
    return 0;
}

by shb20111113 @ 2023-08-05 22:32:02

@yiyezhiqiu1011 参考一下我的代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N]={0}, b[N]={0};
int n, len;
string m;
bool check() {
    for (int i = 0; i < len; i++) {
        if (a[i] != a[len - i - 1]) {
            return false;
        }
    }
    return true;
}
void ps() {
    for (int i = 0; i < len; i++) 
        b[i] = a[len - i - 1];
    for (int i = 0; i < len; i++) 
        a[i] += b[i];
    for (int i = 0; i < len; i++) {
        a[i + 1] += a[i] / n;
        a[i] %= n;
    }
    if (a[len])
        ++len;
}
int main() {
    cin>>n>>m;
    len = m.length();
    for (int i = 0; i < len; i++) {
        if (m[i] >= '0' && m[i] <= '9') {
            a[i] = m[len - 1 - i] - '0';
        } else {
            a[i] = m[len - 1 - i] - 'A' + 10;
        }
    }
    int step;
    for (step = 1; step <= 30; step++) {
        ps();
        if (check())
            break;
    }
    if (step <= 30) {
        cout << "STEP=" << step << endl;
    } else {
        cout << "Impossible!" << endl;
    }
    return 0;
}

来自一个六年级的小学生


|