• 【PAT甲级】1136 A Delayed Palindrome


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1136 A Delayed Palindrome (pintia.cn)
    🔑中文翻译:延迟的回文数
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1136 A Delayed Palindrome

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

    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
    
    • 1

    Sample Output 1:

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

    Sample Input 2:

    196
    
    • 1

    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.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    题意

    这道题是 PAT 1024 那道题的改编,几乎一样。同样想要去获得一个回文数,例如 1234321

    如果给定的数字不是回文数则需要将该数字加上它的逆序数,例如 123 不是回文数,则要加上它的逆序数 321 。然后再判断加上逆序数之后的数是否为回文数,如果还不是就重复上述操作直至是回文数为止。

    但是题目有个限制,上述的加法操作最多只能进行十次,如果十次后还不是回文数就不管了,并且输出 Not found in 10 iterations. ,否则输出 i is a palindromic number. ,其中 i 是最终得到的回文数。

    思路
    1. 这道题给定的数字很大,如果正常加法会爆 int ,需要用到高精度操作,所以一开始我们用字符串类型读入数字,然后再将字符串中的每一位都放进数组当中,注意要从字符转化回数字即 n[i] - '0' 。另外,我们数组中下标为 0 的位置存的是数字的最高位,例如 12345 放到数组中,下标从 04 存的是从 51 的数,即倒着放进数组,这样有利于我们后续的高精度操作。
    2. 先判断该数字是否为回文数,如果不是就进行上述的加法操作。另外,我们将数字反转利用了 c++ 的语法。
    3. 这里套用了高精度的加法模板,其中有些地方可以省略,因为这道题加法的两个数长度是相同的,所以可以不用同时判断 i 小于 ab 的长度。另外,因为 t 是可能会有进位,所以需要判断 t 是否为 0 ,如果不为 0 还需要加到答案数组中。
    4. 在操作的过程中需要输出操作结果,格式为 A + B = C
    5. 根据最后一个数是否是回文数,输出对应的结果。
    代码
    #include
    using namespace std;
    
    //判断是否为回文数
    bool check(vector<int> a)
    {
        for (int i = 0, j = a.size() - 1; i < j; i++, j--)
            if (a[i] != a[j])
                return false;
        return true;
    }
    
    //高精度加法模板
    vector<int> add(vector<int> a, vector<int> b)
    {
        vector<int> c;
        for (int i = 0, t = 0; i < a.size() || i < b.size() || t; i++)
        {
            if (i < a.size())  t += a[i];
            if (i < b.size())  t += b[i];
            c.push_back(t % 10);
            t /= 10;
        }
        return c;
    }
    
    //打印数字
    void print(vector<int> a)
    {
        for (int i = a.size() - 1; i >= 0; i--)
            cout << a[i];
    }
    
    int main()
    {
        string n;
        cin >> n;
        vector<int> a;
        for (int i = n.size() - 1; i >= 0; i--)  a.push_back(n[i] - '0');
    
        for (int i = 0; i < 10; i++)
        {
            if (check(a))    break;	//先判断是否为回文数
            vector<int> b(a.rbegin(), a.rend());	//将数字逆转
            print(a), cout << " + ", print(b), cout << " = ";
            a = add(a, b);	//执行高精度加法操作
            print(a), cout << endl;
        }
    
        //输出对应结果
        if (!check(a))    puts("Not found in 10 iterations.");
        else    print(a), cout << " is a palindromic number." << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    MyBatis
    Java 通配符 在短信发送之中 通配符参数动态获取解决方案
    你写过的最蠢的代码是?——后端篇
    苍穹外卖day8(2)用户下单、微信支付
    HarmonyOS分布式文件系统开发指导
    Oracle中排查谁把表数据删除更新——delete、drop、truncate
    驱动开发day2
    8月份,誉天79名学员通过HCIE认证!
    HTTP相关知识
    mysql 物理备份及恢复
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/126754745