1136. A Delayed Palindrome (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Consider a positive integer N written in standard notation with k+1 digits ai as ak...a1a0 with 0 <= ai < 10 for all i and ak > 0. Then N is palindromic if and only if ai = ak-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. 思路
Pat1024 一样的题目,字符串处理问题。
代码
#include<iostream>
#include<algorithm>
using namespace std; bool isPalindrome(const string& s)
{
for(int i = 0,j = s.size() - 1;i <= j;i++,j--)
{
if(s[i] != s[j])
return false;
}
return true;
} string add(const string& a,const string& b)
{
string tmp;
int len = a.size();
int carry = 0;
for(int i = len - 1;i >= 0;i--)
{
int cur = (a[i] - '0') + (b[i] - '0') + carry;
carry = cur / 10;
cur = cur % 10;
tmp += (to_string(cur));
}
reverse(tmp.begin(),tmp.end());
if(carry > 0)
tmp.insert(0,"1");
return tmp;
} int main()
{
string a;
while(cin >> a)
{
int cnt = 0;
while(!isPalindrome(a) && ++cnt <= 10)
{
string b = a;
reverse(b.begin(),b.end());
string c = add(a,b);
cout << a << " + " << b << " = " << c << endl;
a = c;
}
if(cnt == 11)
cout << "Not found in 10 iterations." << endl;
else
cout << a << " is a palindromic number." << endl;
}
}

  

PAT1136:A Delayed Palindrome的更多相关文章

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

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

  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_A1136#A Delayed Palindrome

    Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...

  9. pat 1136 A Delayed Palindrome(20 分)

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

随机推荐

  1. PR 审批界面增加显示项方法

    PR 审批界面增加显示项 解决方法 Step 1:       进入审批界面: Step 2:       在上图中,点击左下角'About this Page'查看数据源 点击上图中'Expand ...

  2. OpenCV轮廓检测,计算物体旋转角度

    效果还是有点问题的,希望大家共同探讨一下 // FindRotation-angle.cpp : 定义控制台应用程序的入口点. // // findContours.cpp : 定义控制台应用程序的入 ...

  3. Windows下比较简单的获取网页源码的方法

    第一个方法是使用MFC里面的 <afxinet.h> CString GetHttpFileData(CString strUrl) { CInternetSession Session( ...

  4. 带三方登录(qq,微信,微博)

    实现QQ.微信.新浪微博和百度第三方登录(Android Studio) 前言:  对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于 ...

  5. Erlang cowboy 处理不规范的客户端

    Erlang cowboy 处理不规范的客户端 Cowboy 1.0 参考 本章: Dealing with broken clients 存在许多HTTP协议的实现版本.许多广泛使用的客户端,如浏览 ...

  6. windows下mysql免安装配置

    我下载的是mysql-5.5.20-win32.zip版本 1.解压 2.配置环境变量(让系统知道你的bin在哪个位置)path里面设置到安装目录的bin目录 3.复制一个my-huge.ini 另存 ...

  7. android 资源文字ids的作用

    ids.xml--为应用的相关资源提供唯一的资源id.id是为了获得xml中的对象而需要的参数,也就是Object = findViewById(R.id.id_name)中的id_name.这些值可 ...

  8. Libevent库学习笔记

    Libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,Libevent在底层select.pool.kqueue和epoll等机制基础上,封装出一致的事件接口.可 ...

  9. SharePoint2010搜索的简单设置

    1.  开启搜索服务,管理中心 – 应用程序管理 – 服务应用程序 – 管理服务器上的服务 2.  点击进去,启动"SharePoint Foundation搜索"."S ...

  10. prototype.js 和json.js 冲突

    1.冲突简述和分析 prototype.js与json.js并不是完全兼容的.主要冲突在于json.js为Object的原型增加了一个toJSONString的方法. 冲突之一:是prototype中 ...