1136. A Delayed Palindrome (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Consider a positive integer N written in standard notation with k+1 digits ai as ak...a1a0 with 0 <= ai < 10 for all i and ak > 0. Then N is palindromic if and only if ai = ak-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. 思路
Pat1024 一样的题目,字符串处理问题。
代码
#include<iostream>
#include<algorithm>
using namespace std; bool isPalindrome(const string& s)
{
for(int i = 0,j = s.size() - 1;i <= j;i++,j--)
{
if(s[i] != s[j])
return false;
}
return true;
} string add(const string& a,const string& b)
{
string tmp;
int len = a.size();
int carry = 0;
for(int i = len - 1;i >= 0;i--)
{
int cur = (a[i] - '0') + (b[i] - '0') + carry;
carry = cur / 10;
cur = cur % 10;
tmp += (to_string(cur));
}
reverse(tmp.begin(),tmp.end());
if(carry > 0)
tmp.insert(0,"1");
return tmp;
} int main()
{
string a;
while(cin >> a)
{
int cnt = 0;
while(!isPalindrome(a) && ++cnt <= 10)
{
string b = a;
reverse(b.begin(),b.end());
string c = add(a,b);
cout << a << " + " << b << " = " << c << endl;
a = c;
}
if(cnt == 11)
cout << "Not found in 10 iterations." << endl;
else
cout << a << " is a palindromic number." << endl;
}
}

  

PAT1136:A Delayed Palindrome的更多相关文章

  1. PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换

    A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...

  2. PAT 1136 A Delayed Palindrome

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

  3. A1136. Delayed Palindrome

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

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

  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. PAT 1136 A Delayed Palindrome[简单]

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

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

  8. PAT_A1136#A Delayed Palindrome

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

  9. pat 1136 A Delayed Palindrome(20 分)

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

随机推荐

  1. 华为机试题【13】-wave数组找字母游戏

    题目描述: Word Maze 是一个网络小游戏,你需要找到以字母标注的食物,但要求以给定单词字母的顺序吃掉.如上图,假设给定单词if,你必须先吃掉i然后才能吃掉f. 但现在你的任务可没有这么简单,你 ...

  2. 【翻译】在Ext JS中创建特定主题的重写

    Ext JS提供了大量的功能来使类的创建和处理变得简单,还提供了一系列的功能来扩展和重新现有的Javascript类.这意味着可以为类添加行为和创建属于自己的类,或者重写某些函数的行为.在本文,将展示 ...

  3. Xshell Linux 主要命令

    1.   查看当前路径 pwd 2.列出当前目录的文件 ls   列出所有文件或者文件夹 ls  *abc  列出以abc开头的所以文件 ls –l   列出所以文件及其详细详细 3.进入目录 cd  ...

  4. FPGrowth 实现

    在关联规则挖掘领域最经典的算法法是Apriori,其致命的缺点是需要多次扫描事务数据库.于是人们提出了各种裁剪(prune)数据集的方法以减少I/O开支,韩嘉炜老师的FP-Tree算法就是其中非常高效 ...

  5. UTL_HTTP Call a Web Service and Pass Parameters as Part of the URL

    SET DEFINE OFF SET SERVEROUTPUT ON DECLARE req UTL_HTTP.REQ; resp UTL_HTTP.RESP; value VARCHAR2(3276 ...

  6. LESS学习笔记 —— 入门

    今天在网上完成了LESS的基础学习,下面是我的学习笔记.总共有三个文件:index.html.main.less.mian.css,其中 mian.css 是 main.less 经过Koala编译之 ...

  7. java--字符编码,正则表达式

    转载请申明出处:http://blog.csdn.net/xmxkf day21   字符编码 06-IO流(转换流的字符编码) 字符编码: 1. 字符流的出现为了方便操作字符,更重要的是加入了编码转 ...

  8. ruby直接字符串压缩与解压缩

    ruby2.1.3的核心类中包含了Zlib库,其中的Zlib模块包含了对字符串压缩和解压的方法: irb(main):180:0> Zlib.class => Module irb(mai ...

  9. C# 创建Word项目标号列表、多级编号列表

    在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在阅读时,文档也更具美观性.另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档 ...

  10. 基于异步队列的生产者消费者C#并发设计

    继上文<<基于阻塞队列的生产者消费者C#并发设计>>的并发队列版本的并发设计,原文code是基于<<.Net中的并行编程-4.实现高性能异步队列>>修改 ...