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 0≤a?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位的数字就是模拟大数相加啊!!!

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9889496.html

时间: 2024-10-06 09:24:59

PAT 1136 A Delayed Palindrome[简单]的相关文章

pat 1136 A Delayed Palindrome(20 分)

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 0≤a?i??<10 for 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

pat 1136 A Delayed Palindrome

Consider a positive integer N written in standard notation with k+1 digits a?i?? as a?k???a?1??a?0?? with 0≤a?i??<10 for 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 de

1136 A Delayed Palindrome (20 分)

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 0 for all iand 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

PAT Advanced 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 0 for 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.

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 0 <= a~i~ < 10 for 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

PAT World Cup Betting[非常简单]

1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, footbal

PAT 1019 General Palindromic Number[简单]

1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Altho

PAT 1036 Boys vs Girls[简单]

1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contai

PAT 05-1 List Components (简单DFS与BFS)

刚一拿到这道题把他想的太复杂了 明明是长度最大为十的顺序结构就能解决的问题,竟然优先想到用链表. BFS牵扯到一个队列的操作,在这种小规模数据里面 用顺序结构好很多 题目如下: For a given undirected graph with N vertices and E edges, please list all the connected components by both DFS and BFS. Assume that all the vertices are numbered