• 1136 A Delayed Palindrome


    Consider a positive integer N written in standard notation with k+1 digits ai​ as ak​⋯a1​a0​ with 0≤ai​<10 for all i and ak​>0. Then N is palindromic if and only if ai​=ak−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:

    1. 97152 + 25179 = 122331
    2. 122331 + 133221 = 255552
    3. 255552 is a palindromic number.

    Sample Input 2:

    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.

    高精加模拟: 

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. bool check(string x) {
    7. int i = 0;
    8. while (x[i] == '0') {
    9. i++;
    10. }
    11. for (int j = 0; i < x.size(); i++, j++) {
    12. if (x[i] != x[x.size() - 1 - j]) {
    13. return false;
    14. }
    15. }
    16. return true;
    17. }
    18. int main() {
    19. int cnt = 0;
    20. string n;
    21. cin >> n;
    22. while (!check(n) && cnt < 10) {
    23. cnt++;
    24. string t = n, s = "";
    25. reverse(t.begin(), t.end());
    26. int x, y = 0;
    27. for (int i = n.size() - 1; i >= 0; i--) {
    28. x = (n[i] - '0' + t[i] - '0' + y) % 10;
    29. s += (x + '0');
    30. y = (n[i] - '0' + t[i] - '0' + y) / 10;
    31. }
    32. if (y) {
    33. s += (y + '0');
    34. }
    35. reverse(s.begin(), s.end());
    36. int i = 0;
    37. while (s[i] == '0') {
    38. i++;
    39. }
    40. if (i) {
    41. s.erase(0, i);
    42. }
    43. cout << n << " + " << t << " = " << s << endl;
    44. n = s;
    45. }
    46. if (cnt >= 10) {
    47. cout << "Not found in 10 iterations.";
    48. } else {
    49. cout << n << " is a palindromic number.";
    50. }
    51. return 0;
    52. }
  • 相关阅读:
    PC_定点数_模+补码表示+补码相关性质
    在ubuntu20.04上面跑通rocket-chip仿真,用rocket-tools工具
    GateWay网关组件
    跨境电商开店咨询话术
    第三十三章 管理许可(六)
    kubernetes(K8S)集群yaml常见用法
    家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
    pytorch的mask-rcnn的模型参数解释
    Whale 帷幄技术周大咖分享:AI 迎来大洗牌
    2022牛客暑期多校训练营7(总结+补题)
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127959077