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. 嵌入式开发之davinci--- 8148/8168/8127 中的二维图像处理内存tiler 铺瓷砖

    http://blog.csdn.net/shanghaiqianlun/article/details/7619603

  2. NANDflash和NORflash的区别(设计师在使用闪存时需要慎重选择)

    NANDflash和NORflash的区别(设计师在使用闪存时需要慎重选择)     NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底 ...

  3. 微软ASP.NET网站部署指南(8):部署Code-Only更新

    1.  综述 初始化部署以后,你须要继续维护和更新你的网站.本章节将向你展示一个不包含数据库改变的部署升级流程.(下一章节将展示数据库改变的部署升级流程.) 提醒:假设依据本章节所做的操作出现错误信息 ...

  4. 解析IE, FireFox, Opera 浏览器支持Alpha透明的方法

    先请看如下代码: filter:alpha(opacity=50);       /* IE */  -moz-opacity:0.5;              /* Moz + FF */  op ...

  5. 一个格式化字符串的函数ToString

    A Formatting String Function  原文:http://flounder.com/tostring.htm CString ToString(LPCTSTR fmt, ...) ...

  6. php 快速读取文件夹下文件列表

    在读取某个文件夹下的内容的时候 以前是使用 opendir readdir结合while循环过滤 . ..当前文件夹和父文件夹来操作的. 代码如下: 然后偶然发现了有scandir函数 可以扫描文件夹 ...

  7. js获取格式化后的当前时间

    代码如下: function getFormatDate() { var day=new Date(); var Year=0; var Month=0; var Day=0; var Hour = ...

  8. Android之MessageQueue、Looper、Handler与消息循环

    在android的activity中有各种各样的事件,而这些事件最终是转换为消息来处理的.android中的消息系统涉及到: *  消息发送 *  消息队列 *  消息循环 *  消息分发 *  消息 ...

  9. docker使用阿里云镜像仓库

    1:阿里云docker仓库 https://dev.aliyun.com/search.html 2:进去注册帐号后,点击自己的管理中心. 3:在管理中心点击加速器,右边面板会有你的加速地址,右边面板 ...

  10. 简介Objective-C语言

    2011-05-11 11:20 佚名 百度百科 字号:T | T Objective-C,是扩充C的面向对象编程语言.主要使用于Mac OS X和GNUstep这两个使用OpenStep标准的系统, ...