大佬求助

P1015 [NOIP1999 普及组] 回文数

baojiaming01 @ 2023-06-03 19:23:54

#include<bits/stdc++.h>
using namespace std;
int n;
string m;
int ans;
int t;
int huiwen(string s)
{
    string s1=s;
    reverse(s1.begin(),s1.end());
    if(s==s1) return 1;
    else return 0;
}
int zhuanhuan1(char x)
{
    if(x>='0' && x<='9') return x-'0';
    else
    {
        if(x=='A') return 10;
        else if(x=='B') return 11;
        else if(x=='C') return 12;
        else if(x=='D') return 13;
        else if(x=='E') return 14;
        else if(x=='F') return 15;
    }
}
char zhuanhuan2(int x)
{
    if(x>=0 && x<=9)return x+'0';
    else
    {
        if(x==10) return 'A';
        else if(x==11) return 'B';
        else if(x==12) return 'C';
        else if(x==13) return 'D';
        else if(x==14) return 'E';
        else if(x==15) return 'F';
    }
}
string jiafa(string s,string s1)
{
    reverse(s.begin(),s.end());
    reverse(s1.begin(),s1.end());
    int a[101],b[101],c[101];
    for(int i=0; i<=s.size()-1; i++) a[i]=zhuanhuan1(s[i]);
    for(int i=0; i<=s1.size()-1; i++) b[i]=zhuanhuan1(s1[i]);
    int len=max(s.size()-1,s1.size()-1);
    for(int i=0; i<=len; i++)
    {
        c[i]=a[i]+b[i]+c[i];
        c[i+1]=c[i]/10;
        c[i]=c[i]%10;
    }
    len++;
    while(c[len]==0 && len>0) len--;
    for(int i=len; i>=0; i--) s[i]=zhuanhuan2(c[i]);
    return s;
}  
int main()
{
    cin>>n>>m;
    t=huiwen(m);
    while(t==0)
    {
        if(ans>30)
        {
            cout<<"Impossible!";
            return 0;
        }
        ans++;
        m=jiafa(m,m);
        t=huiwen(m);
    }
    cout<<"STEP="<<ans;
    return 0; 
}

蒟蒻看的题解


|