其他分享
首页 > 其他分享> > 1136 A Delayed Palindrome (20 分) (高精度 回文数)

1136 A Delayed Palindrome (20 分) (高精度 回文数)

作者:互联网

添加链接描述
高精度加法判断回文数

#include<bits/stdc++.h>
using namespace std;
bool check(string s){
    string p=s;
    reverse(p.begin(),p.end());
    return p==s;
}
string add_(string a,string b){
    string c;
    int p=0;
    for(int i=0;i<max((int)a.size(),(int)b.size());i++){
        if(i<a.size())p+=a[i]-'0';
        if(i<b.size())p+=b[i]-'0';
        c+=p%10+'0';
        p/=10;
    }
    if(p)c+=p+'0';
    reverse(c.begin(),c.end());
    return c;
}
int main(){
    string s;
    cin>>s;
    int cnt=0;
    while(!check(s)){
        if(cnt==10)break;
        cnt++;
        string p=s;
        reverse(p.begin(),p.end());
        string res=add_(p,s);
        cout<<s<<" + "<<p<<" = "<<res<<endl;
        s=res;
    }
    if(cnt==10)puts("Not found in 10 iterations.");
    else cout<<s<<" is a palindromic number."<<endl;

    return 0;
}

标签:begin,cnt,Palindrome,20,string,int,1136,add,check
来源: https://blog.csdn.net/Minelois/article/details/123075943