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. 在python中定义二维数组

    发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...

  2. linux audit审计(7)--读懂audit日志

    让我们先来构造一条audit日志.在home目录下新建一个目录,然后配置一条audit规则,对这个目录的wrax,都记录审计日志: auditctl -w /home/audit_test -p wr ...

  3. nfs+keepalived高可用

    1台nfs主被服务器都下载nfs.keepalived yum install nfs-utils rpcbind keepalived -y 2台nfs服务器nfs挂载目录及配置必须相同 3.在主n ...

  4. dataTable之自定义按钮实现全表 复制 打印 导出 重载

    //本文对常用表格插件datatable 的自定义按钮功能键进行详细解释//其中 15-78行是定义表单//16 18 19 三行定义自定义功能按钮 实现对全表的 复制 打印 导出(csv即excel ...

  5. 微信小程序支付功能

    API:wx.requestPayment() { } https://blog.csdn.net/qishubiao/article/details/80804052

  6. has invalid type <class 'numpy.ndarray'>, must be a string or Tensor

    转自: https://blog.csdn.net/jacke121/article/details/78833922 has invalid type <class 'numpy.ndarra ...

  7. Js--动态生成表格

    <div>        <h1>动态生成表格</h1>        <div id="table1">            行 ...

  8. Redux学习(3) ----- 结合React使用

    Redux 和React 进行结合, 就是用React 做UI, 因为Redux中定义了state,并且定义了改变或获取state的方法,完全可以用来进行状态管理,React中就不用保存状态了,它只要 ...

  9. HTML5-Input

    HTML5拥有多个新的表单输入类型,这些新特性提供了更好的输入控制和验证(有的浏览器不支持) color.date.datetime.datetime-local.email.month.number ...

  10. 「BZOJ1691」[Usaco2007 Dec] 挑剔的美食家 (Treap)

    Description 与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了.现在,Farmer John不得不去牧草专供商那里 ...