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. 细粒度语义分割:ICCV2019论文解析
  2. modoer点评系统3.5_丰田“奥拓”正式亮相,油耗3.5L,配软顶敞篷+四开门,或4万起...
  3. 【跃迁之路】【545天】程序员高效学习方法论探索系列(实验阶段302-2018.08.04)...
  4. Spring Cloud Alibaba - 14 OpenFeign自定义配置 + 调用优化 + 超时时间
  5. 六十七、SpringBoot嵌入式的Servlet容器和创建war项目
  6. Dalivik垃圾回收收机制Cocurrent GC简介
  7. 使用BeetleX访问redis服务
  8. CF891B-Gluttony【构造】
  9. C++ Builder 启动时不显示主窗体
  10. resnet50网络结构_AAAI2020 | 利用网络结构关系加速NAS+Layer
  11. 金融行业怎么用AI?蚂蚁金服是这么做的
  12. Python 做自动化测试环境搭建
  13. 机器学习 - [源码实现决策树小专题]决策树如何分裂以拓展节点(以及在不允许调用sklearn等库的源代码实现)
  14. Android应用系列:双击返回键退出程序
  15. 科锐c语言,科锐C语言学习视频,资源教程下载
  16. 计算机二级答题技巧口诀,计算机二级考试答题技巧(祝逢考必过)
  17. 2020李宏毅学习笔记——16.Recurrent Netural Network 下
  18. 区块链是什么?简单理解区块链
  19. 关于Optical Zoom
  20. 【蓝桥杯嵌入式主板G4】第五章 利用Delay函数来实现LED的闪烁

热门文章

  1. 示例 Demo 工程和 API 参考链接
  2. PostgreSQL 中的递归查询 与oracle 的比较
  3. matlab 等分矩阵,用matlab根据列拆分矩阵.
  4. python亲密度_859. 亲密字符串(Python)
  5. 计算机硬件系统都是看得见的,计算机组成硬件系统).doc
  6. mysql主从进行扩展_MySQL 主从扩展
  7. java百度云文件上传_关于如何在自己项目集成百度云BCE文件上传STS方案
  8. NYOJ 24 素数距离问题
  9. ubuntu安装好后常用软件安装和配置
  10. python3 Connection aborted.', RemoteDisconnected('Remote end closed connection without response'