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

转载于:https://www.cnblogs.com/wanghao-boke/p/10453492.html

1136 A Delayed Palindrome (20 分)相关推荐

  1. 1136. A Delayed Palindrome (20) 大数模拟

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

  2. PAT甲级1136 A Delayed Palindrome :[C++题解]回文串和高精度并输出过程

    文章目录 题目分析 题目链接 题目分析 此题和PAT甲级1024 Palindromic Number:[C++题解]回文串和高精度加法 一样.区别是多了输出整个计算过程. 下面是主要知识点. 一个判 ...

  3. 1136 A Delayed Palindrome 需再做

    注意点: 1. 大整数即高精度整数,数据结构bign要会定义 2. 记得写构造函数或者通过别的方式初始化bign 3. len属性记得手动更新 4. int d[maxn]数组是顺位存储,意味着字符串 ...

  4. 1136 A Delayed Palindrome

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

  5. pat-1136. A Delayed Palindrome (20) 模拟

    本可以java大数就能搞定的问题 当时用java写完答案明明对了提交总是答案错误 很是无语 题意 任给我们一个数 让我们对这个数进行翻转相加 如果加和是个回文数 就找到了程序出口 如果不是继续迭代 输 ...

  6. 7-1 查找书籍(20 分)(程序设计天梯赛模拟练习题)

    7-1 查找书籍(20 分) 给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价. 输入格式: 输入第一行给出正整数n(<10),随后给出n本书的信息.每本书 ...

  7. PTA—输出全排列 (20分) 递归回溯思想

    PTA-输出全排列 (20分) 递归回溯思想 题目要求: 请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间. 输入格式: 输入给出 ...

  8. 2、求100以内的素数之和。(20分)

    题目: /* 2.求100以内的素数之和.(20分) */ 代码: public class Two207 {public static void main(String[] args) {int s ...

  9. 1、输入四个整数,按照从小到大顺序输出。(20分)

    题目: /* 1.输入四个整数,按照从小到大顺序输出.(20分) */ 代码: 注:题目说的是四个数的排序,我就直接写了个冒泡排序 public class One207 {public static ...

最新文章

  1. 小评 XenServer 6.0功能
  2. 鸿蒙系统公布名单,鸿蒙系统首批升级名单公布_鸿蒙系统首批升级机型
  3. 对于早期Servlet内核你清楚吗,这些基础的东西,不能忘哦
  4. Cere Network将在DAOMaker平台启动首次种子私募轮社区融资
  5. 消息系统Kafka介绍
  6. Word,PDF,PPT,TXT之间的转换方法(
  7. java 省份城市_如何用Java输入一个省份,然后列出这个省份所有城市的名字?
  8. 【收藏】DIABLO 2 CD KEY
  9. spring框架学习(一)
  10. uniapp设置输入框金额效果demo(整理)
  11. 易语言取php网页数据,易语言爬取网页内容方法
  12. 【计算机网络】 如何看懂路由表
  13. 贷款的五种方法,解决您的购房资金问题
  14. socket缓冲区/sk_buffer/滑动窗口关系
  15. 【zhailu】国外产品经理到底在做些什么?【转载内容】
  16. vue封装打印插件print.js实现打印组件功能
  17. java自学——java的基本讲解和变量、字符串、运算符的简单介绍
  18. 响应式思维 (Thinking Reactively) | Ben Lesh
  19. 仿真枪测速器开发之液晶横屏(ST7531)
  20. 感谢这10所高校师生!为边缘计算发展做出贡献!

热门文章

  1. C++实例讲解Binder通信
  2. mysql 5.1由于Host为localhost的用户为空,密码为空,导致本地用户无法登陆。
  3. 各种说明方法的答题格式_语文万能答题公式,给孩子收藏吧!
  4. epic怎么添加本地游戏_游戏日报:原神公测无法上架多家安卓渠道;Epic投资布局游戏UGC平台...
  5. java显示长度和第一个字符_从Java字符串中以长度1的字符串返回的第一个字母的最佳方法是什么?...
  6. pyplot绘制图片_matplotlib系列之pyplot
  7. php论坛思路,PHP论坛实现积分系统的思路代码详解
  8. 红外线遥控c语言程序,红外遥控的C程序
  9. 双纵坐标的绘图命令_工程师绘图必备软件——OriginLab 2019b
  10. android jni示例_Android服务示例