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

pat 1136 A Delayed Palindrome

时间:2018-12-08 20:24:07      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:until   specific   init   opera   -o   following   class   加法   保存   

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



思路:因为他的数字个数不超过1000个,所以用字符串来保存数据;
然后判断是不是回文串,反过来比较是否相同即可;


其中用到了reverse()函数,把一个字符串反过来,大数相加,存进位;因为是加法,所以进位最大也只可能是1;

注意事项:
将单个字符存入字符串:ans += char(num + 0);
将个位数转换为char类型,要 + 0‘;不能 - ‘0’


代码如下:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 
 5 string add(string a)
 6 {
 7     string ans;
 8     int l = a.length();
 9     int co = 0;
10     for(int i = 0;i < l;i++)
11     {
12         int num = (a[i] - 0) + (a[l - 1 - i] - 0) + co;
13         //cout << num << "---" << endl;
14         co = 0;
15         if(num > 9)
16         {
17             co = 1;
18             num -= 10;
19         }
20         ans += char(num + 0);
21     }
22     if(co == 1)
23     ans += 1;
24     reverse(ans.begin(),ans.end());
25     //cout << ans << endl;
26     return ans;
27 }
28 
29 
30 string a,b;
31 int main()
32 {
33     cin >> a;
34     int i,len;
35     for(i = 0;i < 10;i++)
36     {
37         b = a;
38         reverse(b.begin(),b.end());
39         if(a == b)
40         break;
41         cout << a << " + " << b << " = " << add(a) << endl;
42         a = add(a);
43     }
44     if(i == 10)
45     cout << "Not found in 10 iterations." << endl;
46     else
47     cout << a << " is a palindromic number." << endl;
48     return 0;
49 }

 




pat 1136 A Delayed Palindrome

标签:until   specific   init   opera   -o   following   class   加法   保存   

原文地址:https://www.cnblogs.com/lu1nacy/p/10088765.html

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