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~ with 0 <= a~i~ < 10 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 fromhttps://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.

输入数的长度最长有1000位,应该用string来保存输入数据

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 using namespace std;
 5 string add(string a, string b){
 6   int l=a.size(), carry=0;
 7   for(int i=l-1; i>=0; i--){
 8     a[i]=b[i]+a[i]+carry-‘0‘;
 9     if(a[i]>‘9‘){
10       carry=1;
11       a[i] -= 10;
12     }else carry=0;
13   }
14   if(carry==1) a = ‘1‘+a;
15   return a;
16 }
17 int main(){
18   int i;
19   bool flag=true;
20   string n, num, copy;
21   cin>>n;
22   for(i=0; i<10; i++){
23     copy=num=n;
24     reverse(num.begin(), num.end());
25     if(num==n){ cout<<num<<" is a palindromic number."<<endl; flag=false; break;}
26     n=add(n, num);
27     cout<<copy<<" + "<<num<<" = "<<add(copy, num)<<endl;
28   }
29   if(flag) cout<<"Not found in 10 iterations."<<endl;
30   return 0;
31 }

原文地址:https://www.cnblogs.com/mr-stn/p/9161612.html

时间: 2024-11-05 20:40:49

1136 A Delayed Palindrome (20)的相关文章

1136 A Delayed Palindrome (20 分)

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?? with 0 for all iand 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

PAT Advanced 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?? 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.

pat 1136 A Delayed Palindrome(20 分)

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?? with 0≤a?i??<10 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

PAT 1136 A Delayed Palindrome[简单]

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?? with 0≤a?i??<10for 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

pat 1136 A Delayed Palindrome

Consider a positive integer N written in standard notation with k+1 digits a?i?? as a?k???a?1??a?0?? with 0≤a?i??<10 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 de

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

PAT甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain

Linux基础进程命令详解

进程有关基础命令 一.进程定义 1.进程就是CPU未完成的工作,而且它是其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源. 二.Linux系统进程和一些有关进程的命令 1.ps命令       ps  [options]  1>ps - report a snapshot of the current processes.    解释:报告当前系统的进程信息  2>基本参数    a      #关于当前环境的所有进程    -a    #显示当前环境下shell的进程    x 

使用Rman Duplicate执行DataGuard主备重新同步

DataGuard物理主备,当遭遇各种故障导致备库的数据没有与主库同步时,就需要尽快将主备的数据同步起来. 一般的方法是主库全备份,备库nomount还原控制文件,mount还原主库全备数据. 这里Oracle有一个Rman Duplicate功能,可以用于主库与备库数据的快速同步 具体理论就不多说,网上可查,下面实战一遍,看操作步骤和对应的日志,也就自然理解了 1.在主库执行备份,备份脚本如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [[email pro