1136 A Delayed Palindrome (20 分)

要上高精度。

const int N=10010;
struct bignum
{
    int m[N];
    int len;
    bignum()
    {
        memset(m,0,sizeof m);
        len=0;
    }
};
int n;

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

bignum trans(string s)
{
    bignum c;
    c.len=s.size();
    for(int i=0;i<s.size();i++)
        c.m[i]=s[s.size()-1-i]-'0';
    return c;
}

bignum add(bignum a,bignum b)
{
    bignum c;
    c.len=max(a.len,b.len);
    int carry=0;
    for(int i=0;i<c.len;i++)
    {
        int t=a.m[i]+b.m[i]+carry;
        c.m[i]=t%10;
        carry=t/10;
    }
    if(carry) c.m[c.len++]=carry;
    return c;
}

int main()
{
    string s;
    cin>>s;

    bool ok=false;
    for(int i=0;i<10;i++)
    {
        if(check(s))
        {
            ok=true;
            break;
        }
        string sa=s;
        reverse(s.begin(),s.end());
        string sb=s;
        bignum a=trans(sa);
        bignum b=trans(sb);
        bignum c=add(a,b);
        string res;
        for(int i=c.len-1;i>=0;i--)
            res+='0'+c.m[i];
        s=res;
        cout<<sa<<" + "<<sb<<" = "<<s<<endl;
    }

    if(ok) cout<<s<<" is a palindromic number."<<endl;
    else cout<<"Not found in 10 iterations."<<endl;
    //system("pause");
    return 0;
}

posted @ 2021-03-03 22:03  Dazzling!  阅读(48)  评论(0编辑  收藏  举报