1. 程式人生 > >PAT 1136 A Delayed Palindrome[簡單]

PAT 1136 A Delayed Palindrome[簡單]

lan cannot span reset iostream ble orm 轉換成 dia

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<<\n;
        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)<<\n;
        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[簡單]