【PAT甲级 - C++题解】1136 A Delayed Palin

简介: 【PAT甲级 - C++题解】1136 A Delayed Palin

1136 A Delayed Palindrome


Consider a positive integer N written in standard notation with k+1 digits a i a_ia

i

 as a k   ⋯   a 1   a 0 a_k\ ⋯\ a_1\ a_0a

k

 ⋯ a

1

 a

0

 with 0 ≤ a i < 10 0≤a_i<100≤a

i

<10 for all i and a k > 0 a_k>0a

k

>0. Then N is palindromic if and only if a i = a k − i a_i=a_k−ia

i

=a

k

−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.:https://blog.csdn.net/Newin2020/article/details/126754745


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

Sample Output 1:

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

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.

题意

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


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


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


思路

这道题给定的数字很大,如果正常加法会爆 int ,需要用到高精度操作,所以一开始我们用字符串类型读入数字,然后再将字符串中的每一位都放进数组当中,注意要从字符转化回数字即 n[i] - '0' 。另外,我们数组中下标为 0 的位置存的是数字的最高位,例如 12345 放到数组中,下标从 0 到 4 存的是从 5 到 1 的数,即倒着放进数组,这样有利于我们后续的高精度操作。

先判断该数字是否为回文数,如果不是就进行上述的加法操作。另外,我们将数字反转利用了 c++ 的语法。

这里套用了高精度的加法模板,其中有些地方可以省略,因为这道题加法的两个数长度是相同的,所以可以不用同时判断 i 小于 a 和 b 的长度。另外,因为 t 是可能会有进位,所以需要判断 t 是否为 0 ,如果不为 0 还需要加到答案数组中。

在操作的过程中需要输出操作结果,格式为 A + B = C 。

根据最后一个数是否是回文数,输出对应的结果。

代码

#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;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
759 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)