PAT1136:A Delayed Palindrome
1136. A Delayed Palindrome (20)
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的更多相关文章
- PAT-1136(A Delayed Palindrome)字符串处理+字符串和数字间的转换
A Delayed Palindrome PAT-1136 我这里将数字转换为字符串使用的是stringstream字符串流 扩充:将字符串转换为数字可以使用stoi函数,函数头为cstdlib #i ...
- PAT 1136 A Delayed Palindrome
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k ...
- A1136. Delayed Palindrome
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT 1136 A Delayed Palindrome[简单]
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...
- 1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT_A1136#A Delayed Palindrome
Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...
- pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
随机推荐
- android的Binder通信机制java层浅谈-android学习之旅(88)
1.Service Manager的Java代理对象 在Java层中,Service Manager的代理对象类型为ServiceManagerProxy.它继承并且实现了IServiceManage ...
- Effective C++总结
条款01:视C++为一个语言联邦(View C++ as a federation of languages.) C++主要的四个次语言: (1)C.说到底C++仍是以C为基础:(2)Object-O ...
- Xcode出现may cause a leak非忽略的解决方法
前面提到可以把may cause a leak当成安静的美代码忽略掉,但其实还是有另一种方法滴. 你可以用如下代码替换以消除該警告: [xxx performSelector:_cmd withObj ...
- boost::this_thread::sleep_for()死锁
boost::this_thread::sleep_for()会死锁 (金庆的专栏) 发现睡眠1ms很容易死锁.boost::this_thread::sleep_for(boost::chrono: ...
- linux设备驱动程序--类class的实现
#include <linux/module.h> #include <linux/fs.h> #include <linux/sched.h> #include ...
- 配置SharePoint环境加域提示网络名不可用[已解决]
今天去客户给机器做备机,带着装好SharePoint07的机器跑过去了,先做个LAN,然后连上机器开始工作:首先当然是改ip地址,然后都改好了开始加域,加了好几次,发现都不行,提示"指定的网 ...
- 如何将windows格式的图标作为os x应用程序的图标
刚由windows转为os x的同学可能知道,在os x下我们想改变一个app的图标异常简单,直接打开该app的简介,然后将图标文件拖入简介窗口左上角图标方框即可.但是很多童鞋下载了一些图标文件后发现 ...
- rails关于一个Action的多次或多个Action之间共享数据的思路
举一个实际的例子:一个考试页面,总共有20题,每页一题,通过页面下方的"前一题"和"后一题"的提交按钮来跳转题目.如果到最后一题则再产生一个"交卷&q ...
- ubuntu18.04 安装mysql 5.7.22
后台下载,脱离终端控制 后台下载,可以节省ssh资源占用,且不会因为ssh连接断开而导致下载失败,适用于操作远端云服务器 wget -b 启动后台下载 -o 指定logfile(记录下载进度信息) w ...
- Spring Cloud 入门教程 - Eureka服务注册与发现
简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...