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. kettle的日志

    http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html

  2. 多媒体开发之rtsp---rtsp client 端的实现

    http://blog.csdn.net/xyz_lmn/article/details/6055179 java实现 http://www.cnblogs.com/wohexiaocai/p/454 ...

  3. jquery -- body div 和 body>div 的区别

    body  div表示body下的所有div标签都应用样式,不管是body的儿子还是孙子或更孙子 body >div表示只有body的儿子div才应用样式,孙子以后都不应用

  4. "reason":"No handler for type [attachment] declared on field [file]" 最完全解决方案

    0.elasticsearch-mapper-attachments 2.3.4安装 mapper-attachments安装方法分两类,在线和离线: 在线安装 bin/elasticsearch-p ...

  5. 怎么用MathType解决Word公式排版很乱的问题

    现在办公室起草文件,期刊论文投稿.学校试着编辑都要先在Word中编辑好后再打印出来.在Word中编辑这些文本内容时,如果遇到公式就要使用专门的MathType公式编辑器.而有很多人在用MathType ...

  6. uva 11381(神奇的构图、最小费用最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821 思路:首先拆点,每个字符对应的位置拆成i和i+len,然后 ...

  7. iOS开发之-- 从当前隐藏导航界面push到下一个显示导航界面出现闪一下的问题

    在修改项目代码的过程中,遇到一个问题,就是比如主页面的导航栏是隐藏的,但是需要push到别的页面,这个时候,会出现导航栏闪一下的情况, 下面是我写的一种方案,也就是在loadView这个生命周期函数中 ...

  8. virtualbox虚拟机Linux系统与本地windows系统共享文件方法

    转自:http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html

  9. sys.argv 详细用法

    sys.argv 用于获取命令行参数,用法如下: [root@localhost ~]$ cat 1.py #!/usr/bin/env python #-*- coding:utf-8 -*- im ...

  10. 打开cmd闪退

    我们在使用电脑过程中一般会很少用到cmd命令,CMD命令窗口在一些特殊情况时我们会用到,如PING下看网络通不通.在CMD窗口里运行命令如磁盘格式转换,但是有些朋友遇到了这样的问题,在开始运行输入CM ...