PAT甲级2017秋季1136 A Delayed Palindrome (20分)

太过爱你忘了你带给我的痛 2023-02-22 10:42 150阅读 0赞

1136 A Delayed Palindrome (20分)
在这里插入图片描述

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:

  1. 97152
  2. Sample Output 1:
  3. 97152 + 25179 = 122331
  4. 122331 + 133221 = 255552
  5. 255552 is a palindromic number.

Sample Input 2:

  1. 196

Sample Output 2:

  1. 196 + 691 = 887
  2. 887 + 788 = 1675
  3. 1675 + 5761 = 7436
  4. 7436 + 6347 = 13783
  5. 13783 + 38731 = 52514
  6. 52514 + 41525 = 94039
  7. 94039 + 93049 = 187088
  8. 187088 + 880781 = 1067869
  9. 1067869 + 9687601 = 10755470
  10. 10755470 + 07455701 = 18211171
  11. Not found in 10 iterations.
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. string rev(string s) {
  16. reverse(s.begin(), s.end());
  17. return s;
  18. }
  19. string add(string s1, string s2) {
  20. string s = s1;
  21. int carry = 0;
  22. for (int i = s1.size() - 1; i >= 0; i--) {
  23. s[i] = (s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0';
  24. carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10;
  25. }
  26. if (carry > 0) s = "1" + s;
  27. return s;
  28. }
  29. int main() {
  30. string s, sum;
  31. int n = 10;
  32. cin >> s;
  33. if (s == rev(s)) {
  34. cout << s << " is a palindromic number.\n";
  35. return 0;
  36. }
  37. while (n--) {
  38. sum = add(s, rev(s));
  39. cout << s << " + " << rev(s) << " = " << sum << endl;
  40. if (sum == rev(sum)) {
  41. cout << sum << " is a palindromic number.\n";
  42. return 0;
  43. }
  44. s = sum;
  45. }
  46. cout << "Not found in 10 iterations.\n";
  47. return 0;
  48. }
  49. #include <bits/stdc++.h>
  50. using namespace std;
  51. bool ifPalin(string a){
  52. for(int i=0;i<a.length()/2;i++){
  53. if(a[i]!=a[a.length()-i-1])return false;
  54. }
  55. return true;
  56. }
  57. string add(string a, string b) {
  58. string c = a;
  59. int carry = 0;
  60. for (int i = a.size() - 1; i >= 0; i--) {
  61. c[i] = (a[i] - '0' + b[i] - '0' + carry) % 10 + '0';
  62. carry = (a[i] - '0' + b[i] - '0' + carry) / 10;
  63. }
  64. if (carry > 0) c = "1" + c;
  65. return c;
  66. }
  67. int main(){
  68. string a;
  69. int k=0;
  70. cin>>a;
  71. while(!ifPalin(a)) {
  72. string b("");
  73. for(int i=0;i<a.length();i++){
  74. b.push_back(a[a.length()-1-i]);
  75. }
  76. // printf("%s + %s = %s\n",a.c_str(),b.c_str(),add(a,b).c_str());
  77. cout<<a<<" + "<<b<<" = "<<add(a,b)<<endl;
  78. a=add(a,b);
  79. k++;
  80. if(k==10)break;
  81. }
  82. if(k<=10 && ifPalin(a)) cout<<a<<" is a palindromic number.";
  83. else cout<<"Not found in 10 iterations.";
  84. return 0;
  85. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,150人围观)

还没有评论,来说两句吧...

相关阅读