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​​ with 0≤a​i​​<10for 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.

题目大意:给出一个数,是否回文?否则将其反转然后和原数相加,直到加到第10次未出现或者出现。

#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std; //但是属于哪一条地铁,怎么去表示呢?
bool pd(string s){
int sz=s.size();
for(int i=;i<sz/;i++){
if(s[i]!=s[sz-i-])
return false;
}
return true;
}
int toNum(string s){
int a=;
for(int i=;i<s.size();i++){
a=a*+(s[i]-'');
}
return a;
}
string toStr(int a){
string s;
while(a!=){
s+=(a%+'');
a/=;
}
reverse(s.begin(),s.end());
return s;
}
int main(){
string s;
cin>>s;
string t=s;
//reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
//s.reverse();//这个调用不正确
reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
bool flag=false;
int a,b,c;
for(int i=;i<;i++){
if(pd(s)){//如果这个数本来就是答案。
flag=true;break;
}
a=toNum(t);
b=toNum(s);
c=a+b;
cout<<t<<" + "<<s<<" = "<<c<<'\n';
s=toStr(c);
t=s;
reverse(s.begin(),s.end());
}
if(flag){
cout<<t<<" is a palindromic number.";
}else{
cout<<"Not found in 10 iterations.";
}
return ;
}

//本来是这么写的,但是提交只有18分,最后一个测试点过不去:答案错误。

//我明白了,这个的考点是大数相加(1K位),而我却将其转换成int,实在是太不好了。

#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std; string add(string a){
string b=a;
reverse(b.begin(),b.end());
int jin=,s;
for(int i=a.size()-;i>=;i--){
s=(a[i]-'')+(b[i]-'')+jin;
if(s>=){
s%=;
jin=;
}else jin=;
a[i]=s+'';
}
if(jin==){
a=""+a;
}
return a;
} int main(){
string s;
cin>>s;
string t=s;
//reverse(s[0],s[0]+s.size());//那个反转函数是啥来着???
//s.reverse();//这个调用不正确
reverse(s.begin(),s.end());//t是第一个数,s是第二个数.
bool flag=false;
for(int i=;i<;i++){
if(t==s){//如果这个数本来就是答案。
flag=true;break;
}
cout<<t<<" + "<<s<<" = "<<add(t)<<'\n';
t=add(t);
s=t;
reverse(s.begin(),s.end());
}
if(flag){
cout<<t<<" is a palindromic number.";
}else{
cout<<"Not found in 10 iterations.";
}
return ;
}

//终于正确了,这水题花了我好长时间,哭唧唧!你看清题好吗?1000位的数字就是模拟大数相加啊!!!

PAT 1136 A Delayed Palindrome[简单]的更多相关文章

  1. PAT 1136 A Delayed Palindrome

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

  2. pat 1136 A Delayed Palindrome(20 分)

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

  3. PAT甲级:1136 A Delayed Palindrome (20分)

    PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...

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

  7. 1136 A Delayed Palindrome

    题意:略. 思路:大整数相加,回文数判断.对首次输入的数也要判断其是否是回文数,故这里用do...while,而不用while. 代码: #include <iostream> #incl ...

  8. PAT1136:A Delayed Palindrome

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

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

随机推荐

  1. 三分 - HNU 13409 Flowers

    Flowers Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13409&cour ...

  2. 【BZOJ】1689: [Usaco2005 Open] Muddy roads 泥泞的路(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1689 一开始我也想到了贪心,,,策略是如果两个连续的水池的距离小于l的话,那么就将他们链接起来,,, ...

  3. TrustZone——开源库—Linaro—OP-TEE

    想研究安全系统源代码的有福气了.曾经OVOS的代码缺少TA相关的实现. 这次的版本号,基本框架都有了.先看看架构图吧. 几家大公司做的,可能是ST牵头.页面有ST的LOGO. 代码质量较高. 未来也会 ...

  4. 经典SQL基础回顾

    孔子有云:温故而知新,可以为师矣.既然孔老圣人都云了,咱今天就一起来重温一下MS SQL吧.开篇声明一下:大部分都是基础内容,SQL非常熟练的就别浪费您的时间了,因为这年头,大家时间都挺宝贵的.但是如 ...

  5. (转)Spring AOP编程原理、Demo

    转自: http://pandonix.iteye.com/blog/336873/ AOP原理: http://blog.csdn.net/moreevan/article/details/1197 ...

  6. Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 解决办法

    相信很多人都遇到过这个问题,用Android Studio正在运行程序的时候,突然不知道什么原因,报一个找不到application或者找不到activity的错误(java.lang.ClassNo ...

  7. 使用jq实现打印机的效果

    本例中使用的是jq和es6的语法,代码如下: html: <div id="box"> this is test <br/>  这是测试用的 </di ...

  8. 在scrollview中双击定点放大的代码

    双击放大是 iPhone 的一个基本操作,第三方程序里引入这一功能的话,主要是在 scrollview 呈现一张图片或者 PDF 页面时,双击可以放大,主要代码如下 - (void)scrollVie ...

  9. 20分钟成功编写bootstrap响应式页面 就这么简单

    最近发现一个叫 Bootstrap 的好东西,Bootstrap 是现在最流行的响应式 CSS 框架,它以移动设备优先,能够快速适应不同设备.使用它编写响应式页面快捷.方便,而且屏蔽了浏览器差异.使用 ...

  10. cobbler上部署centos系统修改网卡地址成eth0

    编辑cobbler的profile文件:   cobbler profile edit --name=CentOS-7.2-x86_64 --kopts='net.ifnames=0 biosdevn ...