PAT 1136 A Delayed Palindrome[简单]

Posted bluebluesea

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 1136 A Delayed Palindrome[简单]相关的知识,希望对你有一定的参考价值。

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k+1 digits a?i?? as a?k???a?1??a?0?? with 0a?i??<10for all i and a?k??>0. Then N is palindromic if and only if a?i??=a?k?i?? for all i. Zero is written 0 and is also palindromic by definition.

Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

Given any positive integer, you are supposed to find its paired palindromic number.

Input Specification:

Each input file contains one test case which gives a positive integer no more than 1000 digits.

Output Specification:

For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:

A + B = C

where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number -- in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.

Sample Input 1:

97152

Sample Output 1:

97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.

Sample Input 2:

196

Sample Output 2:

196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.

 题目大意:给出一个数,是否回文?否则将其反转然后和原数相加,直到加到第10次未出现或者出现。

#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

//但是属于哪一条地铁,怎么去表示呢?
bool pd(string s){
    int sz=s.size();
    for(int i=0;i<sz/2;i++){
        if(s[i]!=s[sz-i-1])
            return false;
    }
    return true;
}
int toNum(string s){
    int a=0;
    for(int i=0;i<s.size();i++){
        a=a*10+(s[i]-0);
    }
    return a;
}
string toStr(int a){
    string s;
    while(a!=0){
        s+=(a%10+0);
        a/=10;
    }
    reverse(s.begin(),s.end());
    return s;
}
int main(){
    string s;
    cin>>s;
    string t=s;
    //reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
    //s.reverse();//这个调用不正确
    reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
    bool flag=false;
    int a,b,c;
    for(int i=0;i<10;i++){
        if(pd(s)){//如果这个数本来就是答案。
            flag=true;break;
        }
        a=toNum(t);
        b=toNum(s);
        c=a+b;
        cout<<t<<" + "<<s<<" = "<<c<<
;
        s=toStr(c);
        t=s;
        reverse(s.begin(),s.end());
    }
    if(flag){
        cout<<t<<" is a palindromic number.";
    }else{
        cout<<"Not found in 10 iterations.";
    }
    return 0;
}

 

//本来是这么写的,但是提交只有18分,最后一个测试点过不去:答案错误。 

//我明白了,这个的考点是大数相加(1K位),而我却将其转换成int,实在是太不好了。   

 

#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

string add(string a){
    string b=a;
    reverse(b.begin(),b.end());
    int jin=0,s;
    for(int i=a.size()-1;i>=0;i--){
        s=(a[i]-0)+(b[i]-0)+jin;
        if(s>=10){
            s%=10;
            jin=1;
        }else jin=0;
        a[i]=s+0;
    }
    if(jin==1){
        a="1"+a;
    }
    return a;
}

int main(){
    string s;
    cin>>s;
    string t=s;
    //reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
    //s.reverse();//这个调用不正确
    reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
    bool flag=false;
    for(int i=0;i<10;i++){
        if(t==s){//如果这个数本来就是答案。
            flag=true;break;
        }
        cout<<t<<" + "<<s<<" = "<<add(t)<<
;
        t=add(t);
        s=t;
        reverse(s.begin(),s.end());
    }
    if(flag){
        cout<<t<<" is a palindromic number.";
    }else{
        cout<<"Not found in 10 iterations.";
    }
    return 0;
}

 

//终于正确了,这水题花了我好长时间,哭唧唧!你看清题好吗?1000位的数字就是模拟大数相加啊!!!

以上是关于PAT 1136 A Delayed Palindrome[简单]的主要内容,如果未能解决你的问题,请参考以下文章

pat 1136 A Delayed Palindrome(20 分)

PAT 1136 A Delayed Palindrome[简单]

pat 1136 A Delayed Palindrome

PAT Advanced 1136 A Delayed Palindrome (20分)

1136 A Delayed Palindrome (20 分)

1136 A Delayed Palindrome (20)