Source:

PAT_A1136 A Delayed Palindrome (20 分)

Description:

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.

Keys:

  • 快乐模拟

Attention:

  • under algorithm, reverse(s.begin(),s.end());

Code:

 /*
Data: 2019-08-07 19:32:34
Problem: PAT_A1136#A Delayed Palindrome
AC: 17:12 题目大意:
非回文数转化为回文数;
while(! palindrome){
1.Reverse
2.add
输入:
给一个不超过1000位的正整数
输出:
给出每次循环的加法操作,最多10次循环
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; bool IsPali(string s)
{
int len=s.size();
for(int i=; i<len/; i++)
if(s[i] != s[len--i])
return false;
return true;
} string Func(string s1)
{
string s,s2=s1;
reverse(s2.begin(),s2.end());
int carry=;
for(int i=; i<s1.size(); i++)
{
carry += (s1[i]-''+s2[i]-'');
s.insert(s.end(),''+carry%);
carry /= ;
}
while(carry!=)
{
s.insert(s.end(),''+carry%);
carry /= ;
}
reverse(s.begin(),s.end());
printf("%s + %s = %s\n", s1.c_str(),s2.c_str(),s.c_str());
return s;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE string s;
cin >> s;
for(int i=; i<; i++)
{
if(IsPali(s))
{
printf("%s is a palindromic number.\n", s.c_str());
s.clear();break;
}
s = Func(s);
}
if(s.size())
printf("Not found in 10 iterations."); return ;
}

PAT_A1136#A Delayed Palindrome的更多相关文章

  1. PAT1136:A Delayed Palindrome

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

  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 1136 A Delayed Palindrome(20 分)

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

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

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

随机推荐

  1. Java排序算法之冒泡、选择、插入、快速

    JavaSort Java经典排序算法代码 2018-1-26更新:冒泡排序,选择排序,插入排序,快速排序 1. 冒泡排序 特点:效率低,实现简单 思想(从小到大排): 第1个数和第2个数比较,如果第 ...

  2. awr ash addm

    awr ash addm awr addm :基于快照的ash :单独,每秒采样 dbtime=db cpu + wait 柜员忙碌的时间=A做业务的时间+B做业务的时间等待时间=B等待A做业务的时间 ...

  3. Intellij Idea 13:重置设置

    最近在将Windows下的Idea的设置导入到Mac下的时候,一时手贱,点了全部结果发现悲剧了,所有的快捷键都变成和Windows的一样了.于是我就在CMD+C,V和Ctrl+C,V中不断的进行头脑锻 ...

  4. java如何实现替换指定位置的指定字符串的功能

    /**  * @创建日期 2013-07-15  * @创建时间 14:25:59  * @版本号 V 1.0  */ public class CosTest {     public static ...

  5. request.getInputStream() 的两种解析方式

    http://sagewsg.iteye.com/blog/1717923 byte[] bytes = new byte[1024 * 1024]; InputStream is; try { is ...

  6. Linuxpassword破解及grub加密演示

    password破解及grub加密演示 so easy,不可不会! 原理: 通过进入单用户模式(单用户模式也即是仅仅有一个用户能够訪问资源的状态,且单用户模式就是系统处于最原始的状态,大部分服务还未开 ...

  7. java 效率编程 的一些小知识点

    1.在程序中若出现字符串连接的情况.请使用StringBuffer取代String,这样能够降低多次创建String以及垃圾回收所带来的内存消耗 2.尽量使用局部变量. 调用方法时传递的參数以及调用中 ...

  8. erlang Unicode 处理

    最近在使用erlang做游戏服务器,而字符串在服务器编程中的地位是十分重要的,于是便想仔细研究下字符编码,以及erlang下的字符串处理.先从Unicode开始吧.... [Unicode] Unic ...

  9. 如何将unity资源窗体中的文件一下所有折叠/打开

    1.选中父物体 2.按住alt 3.再按下键盘上的左键/右键:此父物体下的所有折叠/打开 或者 alt + LMB  点击所要折叠/打开的父物体左边的小三角

  10. B5248 [2018多省省队联测]一双木棋 状压dp

    这个题当时划水,得了二十分,现在来整一整. 这个题用状压来压缩边界线,然后通过记忆化搜索进行dp.我们可以观察到,其实每次转移,就是把一个1向左移一位.最后的状态设为0. 这其中还要有一个变量来记录谁 ...