码迷,mamicode.com
首页 > 其他好文 > 详细

1136 A Delayed Palindrome (20)

时间:2018-06-09 23:20:40      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:more   sam   cout   process   return   rev   false   out   led   

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 }

 

 

1136 A Delayed Palindrome (20)

标签:more   sam   cout   process   return   rev   false   out   led   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!