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.

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.
#include<iostream>
#include<algorithm>
using namespace std; bool isPalindromic(string &str){
int len = str.size();
for(int i = ; i < len/; i++){
if(str[i] != str[len - i - ])
return false;
}
return true;
} string add(const string &A,const string &B){
string C;
int len = A.size();
int carry = ;
for(int i = len - ; i >= ; i--){
int temp = A[i] - '' + B[i] - '' + carry;
C += temp % +'';
carry = temp / ;
}
if(carry != ) C += carry + '';
reverse(C.begin(),C.end());
return C;
} int main(){
string A,B,C;
cin >> A;
int cnt = ;
if(isPalindromic(A)){
cout << A << " is a palindromic number.";
return ;
}
while(cnt--){
B = A;
reverse(A.begin(),A.end());
C = add(A,B);
cout << B << " + " << A << " = " << C << endl;
if(isPalindromic(C)){
cout << C << " is a palindromic number.";
return ;
}
A = C;
}
cout <<"Not found in 10 iterations.";
return ;
}

1136 A Delayed Palindrome (20 分)的更多相关文章

  1. PAT甲级:1136 A Delayed Palindrome (20分)

    PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...

  2. PAT 1136 A Delayed Palindrome

    1136 A Delayed Palindrome (20 分)   Consider a positive integer N written in standard notation with k ...

  3. pat 1136 A Delayed Palindrome(20 分)

    1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...

  4. PAT 1136 A Delayed Palindrome[简单]

    1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...

  5. 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​​ ...

  6. 1136 A Delayed Palindrome

    题意:略. 思路:大整数相加,回文数判断.对首次输入的数也要判断其是否是回文数,故这里用do...while,而不用while. 代码: #include <iostream> #incl ...

  7. PAT1136:A Delayed Palindrome

    1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. PAT_A1136#A Delayed Palindrome

    Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...

  9. PAT A1136 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​​ ...

随机推荐

  1. git之对比svn

    关于git的发展和历史介绍网上有很多资料,大家可以自行去了解,这里给大家一个传送门git介绍在这里我就不多说了.我们今天本篇文章的定位就是帮助大家来了解一下关于git和svn之间的区别及git的安装. ...

  2. Load-time relocation of shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/08/25/load-time-relocation-of-shared-libraries/ This article ...

  3. 数据库(学习整理)----7--Oracle导入导出数据库文件

    Oracle导入本地数据库操作手册 1.旧数据库忘记了密码,首先进入cmd:1)输入:sqlplus/nolog2)输入:connect/as sysdba3)输入:alter user sys id ...

  4. 用maven将项目安装到本地仓库,为什么老是在默认仓库地址(C:\Users\userName\.m2\repository)

    使用mvn clean install安装项目到本地的时候,在idea中配置好了本地仓库地址,见下图: 但是安装时,还是安装到了C:\Users\userName\.m2\repository路径下, ...

  5. 设计模式08: Composite 组合模式(结构型模式)

    Composite 组合模式(结构型模式) 对象容器的问题在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即他们在充当对象的同时,又是其他对象的容器. public interface I ...

  6. xubuntu14.04LTS安装steam后运行的错误解决办法

    我在ubuntu14.10中没碰到过这个问题,但在xubuntu14.04LTS中碰到 Steam needs to install these additional packages: libgl1 ...

  7. Django实战之古风博客

    感谢 感谢杨青 大大的古风模板,设计的很棒,给个赞. 如有侵权,请联系我 运行环境 python3.6 Django==1.11.4 django-ckeditor==5.4.0 django-js- ...

  8. Django-项目上线后,静态文件配置失效以及404、500页面的全局配置

    https://blog.csdn.net/Jamin2018/article/details/79060509 https://www.cnblogs.com/lfoder/p/6013142.ht ...

  9. rsync服务搭建--2018.5.8 [优化后最终版]

    2018年5月8日 22:09:38 第一步配置基础环境(按照自己的规划配置并非每人的环境都一致) 第一台服务器(RSYNC服务器): rsync外网地址:10.0.0.41  rsync内网地址:1 ...

  10. python+echarts==pycharts

    Django数据可视化 pyechats http://pyecharts.org/#/zh-cn/django