PAT甲级1136题(延迟回文数)


1.题目介绍

1136

2.考察点,难度

高精度题,string字符串处理,难度易

3.解题代码

#include <bits/stdc++.h>
using namespace std;

int main(){

    string s,k,sum;
    int cnt=0;
    cin>>s;
    k=s;
    reverse(k.begin(),k.end());
    while(k!=s && cnt<10){
        cout<<s<<" + "<<k<<" = ";
        int flag=0;
        sum="";
        for(int i=0;i<s.length();i++){
            sum = sum+to_string((flag + s[i]-'0' + k[i]-'0')%10);
            flag=flag + s[i]-'0' + k[i]-'0')/10;
        }
        if(flag)    sum=sum+to_string(flag);
        reverse(sum.begin(),sum.end());
        cout<<sum<<endl;
        cnt++;
        s=k=sum;
        reverse(k.begin(),k.end());
    }
    if(cnt>=10   && k!=s)  cout<<"Not found in 10 iterations."<<endl;
    else    cout<<s<<" is a palindromic number."<<endl;
}

4.原题地址

https://pintia.cn/problem-sets/994805342720868352/problems/994805345732378624


文章作者: Peyton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Peyton !
  目录