第二个WA了

P1015 [NOIP1999 普及组] 回文数

hehuan_haitang @ 2023-04-29 21:19:44

//问题在哪里?
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

int n;
bool is_(vector<int> &A)
{
    auto B = A;
    reverse(B.begin(), B.end());

    return A == B;
}

vector<int> add(vector<int> &A, vector<int> &B)
{
    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
        t += A[i];
        t += B[i];

        C.push_back(t % n);
        t /= n;
    }

    if (t) C.push_back(t);

    return C;
}

int main()
{
    vector<int> A = {};
    string a;
    cin >> n >> a;
    for (int i = a.size() - 1; i >= 0; i -- ) 
    {
        if ('0' <= a[i] && a[i] <= '9') A.push_back(a[i] - '0');
        else A.push_back(a[i] - '@');
    }

    if (is_(A)) 
    {
        cout << "STEP=0";
        return 0;
    }
    // cout << endl;
    // for (int i = A.size() - 1; i >= 0; i -- )
    // {
    //     printf("%d", A[i]);
    // }
    // cout << endl;
    auto B = A;
    reverse(B.begin(), B.end());

    int i;
    for (i = 1; i <= 30; i ++ )
    {
        A = add(A, B);

        // for (int i = A.size() - 1; i >= 0; i -- )
        // {
        //     printf("%d", A[i]);
        // }
        // cout << endl;

        if (is_(A)) break;
        B = A;
        reverse(B.begin(), B.end());
    }

    if(i <= 30) printf("STEP=%d", i);
    else printf("Impossible!");

    return 0;
}

by SCP5543 @ 2023-05-14 08:56:47

试试这个

#include <cstdio>
#include <cstring>
const int S=303;//一次加法顶多多一位,所以顶多多30位,也就是130位左右。我开大一点,开到300.
int n,a[S],l;
char c[S],d[S];
inline void add()
{
    for (int i=0;i<l;++i)
        d[l-i-1]=c[i];
    l+=2;//可能有进位,所以我们干脆在前面先多空个两位
    for (int i=0;i<l;++i)
    {
        c[i]+=d[i];
        if (c[i]>=n) c[i+1]++,c[i]-=n;
    }
    while (!c[l-1]) --l;//大不了多余的前导0再减回来嘛~~简化思维~~
}
inline bool pd()
{
    for (int i=0;i<l;++i)
        if (c[i]!=c[l-1-i]) return false;
    return true;
}
int main()
{
    scanf("%d%s",&n,c);l=strlen(c);
    for (int i=0;i<l;++i)
    {
        if (c[i]>='0' && c[i]<='9') c[i]-='0';
        else c[i]=c[i]-'A'+10;
    }
    int step=0;
    while (!pd())
    {
        ++step;
        if (step>30) break;
        add();
    }
    if (step<=30) printf("STEP=%d\n",step);
    else puts("Impossible!");
    return 0;
}

by beicaoyang @ 2023-07-04 12:00:21

把输入部分的‘@’改成55或‘7’就好了


|