【PAT甲级 - C++题解】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

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.
题意

这道题是 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<bits/stdc++.h>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值