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

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 Cbecomes 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.
 #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int k = ;
string str1, str2, str;
cin >> str;
while (k < )
{
int c = ;
str1 = str2 = str;
reverse(str2.begin(), str2.end());
if (str1 == str2)
break;
cout << str1 << " + " << str2 << " = ";
for (int i = str1.length()-; i>=; --i)
{
str[i] = (str1[i] - '' + str2[i] - '' + c) % + '';
c = (str1[i] - '' + str2[i] - '' + c) / ;
}
if (c > )
str.insert(str.begin(), , c + '');
cout << str << endl;
k++;
}
if (k == )
cout << "Not found in 10 iterations." << endl;
else
cout << str1 << " is a palindromic number." << endl;
return ;
}

PAT甲级——A1104 Sum of Number Segments【20】的更多相关文章

  1. PAT甲级——A1104 Sum of Number Segments

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  2. PAT Advanced A1104 Sum of Number Segments (20) [数学问题]

    题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...

  3. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  4. PAT 甲级 1104 sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

  5. PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...

  6. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  7. PAT A1104 Sum of Number Segments (20 分)——数学规律,long long

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  8. 【PAT甲级】1104 Sum of Number Segments (20 分)

    题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  9. PAT (Advanced Level) 1104. Sum of Number Segments (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

随机推荐

  1. scala中函数简单使用记录

    object funcHighLevel { def main(args: Array[String]): Unit = { // 创建一个匿名函数 val sayFunc = (name: Stri ...

  2. 在IDEA安装SonarLint插件的步骤和使用方法

    1.安装SonarLint插件方式 2.使用方式 3.效果

  3. thinkphp 性能调试

    开发过程中,有些时候为了测试性能,经常需要调试某段代码的运行时间或者内存占用开销,系统提供了G方法可以很方便的获取某个区间的运行时间和内存占用情况. 例如: 富瑞联华大理石平台大理石平台检定规程 G( ...

  4. 配置Tomcat-8.5 JVM内存参数

    配置Tomcat-8.5 JVM内存参数 apache-tomcat-8.5与之前的版本存在些许差异,配置方式有所改变,并且针对JVM一些参数不再支持.故本文档主要简介一下如何在apache-tomc ...

  5. 学习笔记——CDQ分治

    再次感谢这位大佬的博客:https://www.cnblogs.com/ljc20020730/p/10395866.html CDQ分治,是一种在分治合并中计算前面值对后面答案的贡献的一种算法.今天 ...

  6. Codeforces 1174A Ehab Fails to Be Thanos

    题目链接:codeforces.com/problemset/problem/1174/A 题意:给定长度2n 的数组,改变数组顺序,使得前 n 个数的和不等于 后 n 个数的和,不行输出-1. 思路 ...

  7. IDEA @Autowired dao大红波浪线

    SptingBoot+Mybatis开发通常在dao层的注解是@Mapper 这样每次在ServiceImpl层加注解@Autowired时,注入的dao总是波浪线烦人,其实并没有错,只是idea你太 ...

  8. mysql分区管理语句

    1.key分区语句: ALTER TABLE order_info PARTITION BY KEY(orderSn) PARTITIONS 127; 2.rang分区语句: ALTER TABLE ...

  9. Hadoop搭建,上传文件时出现错误,没有到主机的路由

    解决方案:(1)从namenode主机ping其它slaves节点的主机名(注意是slaves节点的主机名),如果ping不通,原因可能是namenode节点的/etc/hosts 未配置主机名与IP ...

  10. canvas填充规则,非零环绕

    1.看一块区域是否填充 2.从这个区域拉一条直线 3,看和这条直线相交的轨迹 4.如果顺时针轨迹+1 5.如果逆时针轨迹-1 6.所有轨迹的值计算出来 7.如果是非0,那么填充 8.如果是0那么不填充