题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805261754023936
也是一遍过,不过这题有几个注意点
判断回文数点方法,直接用reverse转换后双等号就行了

代码

  1. #include<algorithm>
  2. #include<string>
  3. #include<cstdio>
  4. #include<iostream>
  5. using namespace std;
  6. string add(string a){
  7. string b = a, res = "";
  8. reverse(b.begin(), b.end());
  9. int carry = 0;
  10. for(int i = a.size() - 1; i >= 0; i--){
  11. int temp = (a[i] - '0' + b[i] - '0' + carry) % 10;
  12. carry = (a[i] - '0' + b[i] - '0' + carry) / 10;
  13. res += '0' + temp;
  14. }
  15. if(carry != 0) res += '0' + carry;
  16. reverse(res.begin(), res.end());
  17. return res;
  18. }
  19. bool judge(string a){
  20. string b = a;
  21. reverse(b.begin(), b.end());
  22. if(b == a) return true;
  23. else return false;
  24. }
  25. int main(){
  26. string a;
  27. cin>>a;
  28. int count = 0;
  29. if(judge(a)) cout<< a + " is a palindromic number.";
  30. else {
  31. while(!judge(a)){
  32. if(count == 10) {
  33. cout<<"Not found in 10 iterations.";
  34. break;
  35. }
  36. string rev_a, add_a;
  37. rev_a = a;
  38. reverse(rev_a.begin(), rev_a.end());
  39. add_a = add(a);
  40. cout<<a + " + " + rev_a + " = " + add_a<<endl;
  41. if(judge(add_a)){
  42. cout<< add_a + " is a palindromic number.";
  43. break;
  44. }
  45. a = add_a;
  46. count++;
  47. }
  48. }
  49. return 0;
  50. }