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 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 }


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

时间: 2024-09-29 13:06:58

pat 1136 A Delayed Palindrome的相关文章

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

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.

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

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

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

hdu1513——Palindrome

Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3281    Accepted Submission(s): 1136 Problem Description A palindrome is a symmetrical string, that is, a string read identically from

PAT 1009 说反话 C语言

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格. 输出格式:每个测试用例的输出占一行,输出倒序后的句子. 输入样例: Hello World Here I Come 输出样例: Come I Here World Hello 1 #include<stdio.h> 2 #