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

PAT_A1136#A Delayed Palindrome的更多相关文章

  1. PAT1136:A Delayed Palindrome

    1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. PAT 1136 A Delayed Palindrome

    1136 A Delayed Palindrome (20 分)   Consider a positive integer N written in standard notation with k ...

  3. A1136. Delayed Palindrome

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  4. PAT A1136 A Delayed Palindrome (20 分)——回文,大整数

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  5. 1136 A Delayed Palindrome (20 分)

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  6. PAT 1136 A Delayed Palindrome[简单]

    1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...

  7. 1136 A Delayed Palindrome (20 分)

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  8. pat 1136 A Delayed Palindrome(20 分)

    1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...

  9. PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换

    A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...

随机推荐

  1. Spring MVC 入门(一)

    什么是 Spring MVC 学习某一样东西之前,我们一定要大致知道这个东西是什么,能干什么,为什么要用它. Spring MVC 是一个开源平台,一个基于 Spring 的 MVC 框架,它支持基于 ...

  2. MVC.Net: jqueryval错误

    当使用Mvc.net创建Create表单后,firebug Create页面会出现404 Not Found - http://192.168.3.95:7001/bundles/jqueryval& ...

  3. QPS计算

    案例:公司xiaoyb性能测试评估 我们预估支持500家学校,每所学校300人,每天有10%的活跃率,每天有500*300*10%=15000人,每人每天平均请求20次,每天大概请求时间共8小时,80 ...

  4. 自定cell(XIB)团购思路

    自定cell(XIB)团购思路 步骤一.先解析plist文件,创建model层数据. - (instancetype)initWithDict:(NSDictionary *)dict {     s ...

  5. oc52--autorelease1

    // // main.m /* autorelease也是用于内存管理的,给对象发送autorelease消息就会把对象放入autoreleasepool这个池子中,当池子销毁的时候会对池子里面的所有 ...

  6. Effective Java(二)—— 循环与 StringBuilder

    当需要为一个类编写 toString() 方法时,如果字符串操作比较简单,便可以信赖编译器,它会为你合理地构造最终的字符串结果(而不会不断创建冗余的中间变量). String mongo = &quo ...

  7. 【IDEA】(4)---很好用的DEBUG功能

    IDEA-DEBUG功能 一.常用快捷键 快捷键并不是完全一样的,我这边是MAC安装的IDEA, 这边最主要还是知道DEBUG时常用的功能. 1.快捷键 F7 #进入下一步,如果当前行是一个方法,则进 ...

  8. epoll实现reactor模式

  9. web 应用

    一.web应用 web应用程序是一种可以通过web访问的应用程序,程序 的最大好处是用户很容易访问应用程序,用户只需要有浏览器 即可,不需要安装其他团建,用用程序有两种模式C/S.B/S.C/S是客户 ...

  10. RecyclerView 通用 Adapter

    先看使用方法