25分,计算时出了奇怪的问题

P1015 [NOIP1999 普及组] 回文数

LucaZSC @ 2022-08-14 16:41:39

#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
long long n,a[300005],m,b[300005];
string as;
bool hui(long long n){
    for(int i=0;i<n;i++){
        if(a[i]!=a[n-i-1])return 0;
    }
    return 1;
}
int main(){
    cin>>m>>as;
    n=as.size();
    for(int i=0;i<n;i++){
        a[i]=as[i]-'0';
    }
    for(int ii=1;ii<=30;ii++){
        if(hui(n)){
            cout<<"STEP="<<ii;
            return 0;
        }
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++){
            b[i]=a[i];
        }
        for(int i=0;i<n;i++){
            a[i]+=b[n-i-1];
        }
        for(int i=0;i<n;i++){
            a[i+1]+=a[i]/m;
            a[i]%=m;
        }
        if(a[n])n++;
    }
    cout<<"Impossible!";
    return 0;
}

by myyyIisq2R @ 2022-08-22 11:41:16

  1. 注意特判16进制。
  2. 观察样例,ii减去1后再输出。
#include <bits/stdc++.h>
using namespace std;
long long n, a[300005], m, b[300005];
string as;

bool hui(long long n)
{
    for (int i = 0; i < n; i++)
    {
        if (a[i] != a[n - i - 1])
            return 0;
    }
    return 1;
}

int main()
{
    cin >> m >> as;
    n = as.size();
    for (int i = 0; i < n; i++)
    {
        if (as[i] >= '0' && as[i] <= '9')
            a[i] = as[i] - '0';
        else
            a[i] = as[i] - 'A' + 10;
    }
    for (int ii = 1; ii <= 30; ii++)
    {
        if (hui(n))
        {
            cout << "STEP=" << ii - 1;
            return 0;
        }
        memset(b, 0, sizeof(b));
        for (int i = 0; i < n; i++)
        {
            b[i] = a[n - i - 1];
        }
        for (int i = 0; i < n; i++)
        {
            a[i] += b[i];
        }
        for (int i = 0; i < n; i++)
        {
            a[i + 1] += a[i] / m;
            a[i] %= m;
        }
        if (a[n])
            n++;

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

by myyyIisq2R @ 2022-08-22 11:41:53

@wangmingwei 或者初始化 ii 为0


by LucaZSC @ 2022-09-10 12:05:19

才看见

谢谢@wangmingwei dalao


|