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:

  1. 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:

  1. 97152

Sample Output 1:

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

Sample Input 2:

  1. 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 <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. int k = ;
  8. string str1, str2, str;
  9. cin >> str;
  10. while (k < )
  11. {
  12. int c = ;
  13. str1 = str2 = str;
  14. reverse(str2.begin(), str2.end());
  15. if (str1 == str2)
  16. break;
  17. cout << str1 << " + " << str2 << " = ";
  18. for (int i = str1.length()-; i>=; --i)
  19. {
  20. str[i] = (str1[i] - '' + str2[i] - '' + c) % + '';
  21. c = (str1[i] - '' + str2[i] - '' + c) / ;
  22. }
  23. if (c > )
  24. str.insert(str.begin(), , c + '');
  25. cout << str << endl;
  26. k++;
  27. }
  28. if (k == )
  29. cout << "Not found in 10 iterations." << endl;
  30. else
  31. cout << str1 << " is a palindromic number." << endl;
  32. return ;
  33. }

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. idea-----idea中“cannot resolve symbol servlet”的解决

    原文章链接: 传送器>>>>>>>>>>>>>>>>. 第一次使用IntelliJ IDEA时我遇到了& ...

  2. C#反射从入门到放弃(这部分遇到的新东西太多了让人接受不能)

    首先,我们需要知道type,type是类型的类型(笑 官方点的说法是,BCL声明了一个Type抽象类,它被设计用来包含类型的特性, 使用这个类的对象(抽象类的对象?这显然是错误的,但是这里用的其实是T ...

  3. 从psd图中将图层导出成单独文件

  4. webstorm常见快捷方法与遇到的一些问题

    1.动态添加标签快捷写法 例子:生成10个文字按顺序编号的class为task-item的div 2.win10下webstorm的terminal无法输入? 打开一个 cmd.exe,标题栏 右键 ...

  5. thinkphp 储存驱动

    存储驱动完成了不同环境下面的文件存取操作,也是ThinkPHP支持分布式和云平台的基础. 默认的存储驱命名空间位于Think\Storage\Driver,每个存储驱动必须继承Think\Storag ...

  6. mysql删除字段为null的数据

    delete FROM main_bussiness_cost1 where date is null; 不能用 date = null:

  7. Linux date命令 crontab每个月最后一天

    ###使用date获取日期时间等 # 当前日期 openstack@ubuntu:~$ date 2019年 01月 15日 星期二 15:10:49 CST # 明天 openstack@ubunt ...

  8. Vue.js框架的基础指令

    Vue.js 渐进式 javascript 框架,可以独立完成前后端分离式web项目的javascript框架 js是页面脚本语言,用来控制或是辅助页面搭建,vue是js功能的集合体. 三大主流前端框 ...

  9. jquery控件的学习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. scala 常用模式匹配类型

    模式匹配的类型 包括: 常量模式 变量模式 构造器模式 序列模式 元组模式 变量绑定模式等. 常量模式匹配 常量模式匹配,就是在模式匹配中匹配常量 objectConstantPattern{ def ...