Source:

PAT_A1136 A Delayed Palindrome (20 分)

Description:

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

Keys:

  • 快乐模拟

Attention:

  • under algorithm, reverse(s.begin(),s.end());

Code:

 /*
Data: 2019-08-07 19:32:34
Problem: PAT_A1136#A Delayed Palindrome
AC: 17:12 题目大意:
非回文数转化为回文数;
while(! palindrome){
1.Reverse
2.add
输入:
给一个不超过1000位的正整数
输出:
给出每次循环的加法操作,最多10次循环
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; bool IsPali(string s)
{
int len=s.size();
for(int i=; i<len/; i++)
if(s[i] != s[len--i])
return false;
return true;
} string Func(string s1)
{
string s,s2=s1;
reverse(s2.begin(),s2.end());
int carry=;
for(int i=; i<s1.size(); i++)
{
carry += (s1[i]-''+s2[i]-'');
s.insert(s.end(),''+carry%);
carry /= ;
}
while(carry!=)
{
s.insert(s.end(),''+carry%);
carry /= ;
}
reverse(s.begin(),s.end());
printf("%s + %s = %s\n", s1.c_str(),s2.c_str(),s.c_str());
return s;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE string s;
cin >> s;
for(int i=; i<; i++)
{
if(IsPali(s))
{
printf("%s is a palindromic number.\n", s.c_str());
s.clear();break;
}
s = Func(s);
}
if(s.size())
printf("Not found in 10 iterations."); return ;
}

最新文章

  1. MYSQL远程登录权限设置 ,可以让Navicat远程连接服务器的数据库
  2. mysql深入浅出的笔记(存储过程一)
  3. BFC之浅析篇
  4. Linux系统性能分析
  5. Microsoft Azure 01 (Summarize)
  6. Mysql在高并发情况下,防止库存超卖而小于0的解决方案
  7. pl/sql developer——instant-client 简单配置
  8. (Python)集合、集合的函数
  9. 内部通信服务Factory(WCF)
  10. 【转】 ios开发之倒计时实现的两种方法
  11. C#实现下载功能,可用于软件自动更新
  12. SQL整理5
  13. 微型orm框架--dapper的简单使用
  14. mobiscroll2.5.4 日期组件
  15. github---无命令可视化界面操作
  16. MyBatis - 10.MyBatis扩展
  17. 【Java基础】10、Java中throw和throws的区别
  18. ***PHP基于H5的微信支付开发详解(CI框架)
  19. cors跨域问题
  20. 一个非常有意思的蜜罐T-Pot 16.10

热门文章

  1. java中静态资源处理方法
  2. springmvc 中开发Server Send Event
  3. 获取url地址
  4. UNIX环境高级编程之第3章:文件I/O
  5. FZU 1894 志愿者选拔【单调队列】【monotone decreasing queue】
  6. RabbitMQ基本管理(下)
  7. B1588 [HNOI2002]营业额统计 set||平衡树
  8. Sequence(优先队列)
  9. curl强制下载文件
  10. Appium - xpath