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. Laravel 5.1 中创建自定义 Artisan 控制台命令实例教程

    1.入门 Laravel通过Artisan提供了强大的控制台命令来处理非浏览器业务逻辑.要查看Laravel中所有的Artisan命令,可以通过在项目根目录运行: php artisan list 对 ...

  2. (转)FS_S5PC100平台上Linux Camera驱动开发详解(一) .

     平台linuxstructlinux内核videocam 说明:        理解摄像头驱动需要四个前提:        1)摄像头基本的工作原理和S5PC100集成的Camera控制器的工作原理 ...

  3. CONTEST23 2014xmoi邀请赛(被pj虐哭)

    http://www.luogu.org/contest/show?tid=23 表示被普及的虐瞎了T_T_T_T_T_T_T_TT_T_T_T_T_T_T_T_T 33名100多分滚粗 各种贪心神题 ...

  4. 【BZOJ】1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1639 同tyvj1359,http://www.cnblogs.com/iwtwiioi/p/394 ...

  5. 将spark默认日志log4j替换为logback

    1.将jars文件夹下apache-log4j-extras-1.2.17.jar,commons-logging-1.1.3.jar, log4j-1.2.17.jar, slf4j-log4j12 ...

  6. hdu 4291(矩阵+暴力求循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...

  7. XML Publiser For Excel Template

    1.XML Publisher定义数据 2.XML Publisher定义模板 模板类型选择Microsoft Excel,默认输出类型选择Excel,上传.xls模板 3.定义并发程序 4.定义请求 ...

  8. 0.0.0.0:80端口被系统System占用

    用netstat -ano命令查看,可以找到0.0.0.0:80占用的PID假设为4,然后在任务管理器中的详细信息面板中可以找到PID等于4的进程,如果用户名为SYSTEM, 不能直接终止掉,需要去注 ...

  9. 基于openssl的https服务配置

    环境: CA服务器:192.168.1.121 WEB服务器: 192.168.1.107 一.在CA服务器上生成自签证书 1.生成根私钥 (umask 077;openssl genrsa -out ...

  10. VS2013新特性

    大家可能看到我这边颜色和字体和原本不同,这里特意分享给大家背景护眼色值(这对每天看电脑的程序员很重要对不对!)还有字体: 工具-选项-字体和颜色:在项背景点击自定义-色调85 饱和度123 亮度205 ...