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>
#include<string.h>
using namespace std;
typedef struct NODE{
int num[], len;
NODE(){
fill(num, num + , );
len = ;
}
}bign;
void add(bign &a, bign &b, bign &c){
c.len = ;
b.len = ;
for(int i = a.len - ; i >= ; i--){
b.num[b.len++] = a.num[i];
}
int carry = ;
int i;
for(i = ; i < b.len && i < a.len; i++){
int sum = a.num[i] + b.num[i] + carry;
carry = sum / ;
c.num[c.len++] = sum % ;
}
while(i < b.len){
int sum = carry + b.num[i];
c.num[c.len++] = sum % ;
carry = sum / ;
}
while(i < a.len){
int sum = carry + a.num[i];
c.num[c.len++] = sum % ;
carry = sum / ;
}
if(carry != ){
c.num[c.len++] = carry;
}
}
int isReverse(bign a){
for(int i = , j = a.len - ; i <= j; i++, j--){
if(a.num[i] != a.num[j])
return ;
}
return ;
}
int main(){
char ss[];
scanf("%s", ss);
bign a, b, c;
for(int i = strlen(ss) - ; i >= ; i--){
a.num[a.len++] = ss[i] - '';
}
int tag = ;
if(isReverse(a)){
for(int i = a.len - ; i >= ; i--){
printf("%d", a.num[i]);
}
printf(" is a palindromic number.");
return ;
}
for(int i = ; i < ; i++){
add(a,b,c);
for(int k = a.len - ; k >= ; k--){
printf("%d", a.num[k]);
}
printf(" + ");
for(int k = b.len - ; k >= ; k--){
printf("%d", b.num[k]);
}
printf(" = ");
for(int k = c.len - ; k >= ; k--){
printf("%d", c.num[k]);
}
printf("\n");
if(isReverse(c)){
tag = ;
for(int j = c.len - ; j >= ; j--){
printf("%d", c.num[j]);
}
printf(" is a palindromic number.");
break;
}else{
for(int k = ; k < c.len; k++){
a.num[k] = c.num[k];
}
a.len = c.len;
}
}
if(tag == ){
printf("Not found in 10 iterations.\n");
}
cin >> ss;
return ;
}

总结:

1、大整数相加的问题。要注意的是,在a+b做完之后,要注意检查carry是否为0,如果不为0的话,需要再把carry加上。

2、1230的相反是0123而不是123,所以本题相当于两个待加的数字位数都相同,所以可以直接用两个string做加法,比使用大整数模拟要快一些。

A1136. Delayed Palindrome的更多相关文章

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

  2. PAT1136:A Delayed Palindrome

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

  3. PAT 1136 A Delayed Palindrome

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

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

  5. PAT 1136 A Delayed Palindrome[简单]

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

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

  7. PAT_A1136#A Delayed Palindrome

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

  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. centos6.5安装配置NTP,集群各机器间时间同步

    试验环境 提君博客原创 >>提君博客原创  http://www.cnblogs.com/tijun/  << IP 主机名 角色 描述 同步方式 192.168.11.11 ...

  2. 【python练习题】程序4

    # 题目:输入某年某月某日,判断这一天是这一年的第几天? import time year = input('输入年份: \n') month = input('输入月份: \n') day = in ...

  3. Build 2017 Revisited: .NET, XAML, Visual Studio

    For the next couple months we're going to revisit Build 2017, each post focusing on different aspect ...

  4. h.264并行熵解码

    在前面讨论并行解码的章节中,我们专注于讨论解码的宏块重建部分,甚至把宏块重建描述成宏块解码,这是因为在解码工作中,宏块重建确实占了相当大的比重,不过解码还包含其它的部分,按照解码流程可粗略分为: 读取 ...

  5. BZOJ4514[Sdoi2016]数字配对——最大费用最大流

    题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...

  6. BZOJ2738 矩阵乘法(整体二分+树状数组)

    单个询问二分答案即可,多组询问直接整体二分再二维BIT.注意保证复杂度. #include<iostream> #include<cstdio> #include<cma ...

  7. 查询SQL执行情况

    /* 查询SQL执行情况 包含逻辑读取信息,执行信息等情况*/ SELECT creation_time N'语句编译时间' ,last_execution_time N'上次执行时间' ,total ...

  8. Task Schedule HDU - 3572(按时间点建边)

    问题描述 我们的几何公主XMM已经开始研究计算几何学,专注于她新开的工厂.她的工厂引进了M台新机器来处理即将到来的N个任务.对于第i个任务,工厂必须在第Si天或之后开始处理它,处理Pi天,并在Ei之前 ...

  9. 解决 phpstorm 运行卡,自动关闭等问题

    解决 phpstorm 自动关闭问题: 使用文件搜索工具(可在本博客搜索“管理工具”,或查找安装目录) 找到phpstorm.vmoptions文件,使用记事本打开. 添加以下两行代码: -Dawt. ...

  10. PHP linux ZendGuardLoader.so: undefined symbol: executor_globals

    /usr/xxx/php    xxx/xxx.php 报了这个错. 本人出现此问题的原因:  php执行程序路径错了. 解决: linux下执行   which php   命令  查看php真实路 ...