1136 A Delayed Palindrome (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0 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.
分析:大数模拟,最后一个测试点应该是大数边界,因为数可能是1000位的,所以如果用int 和long long 都会超出范围。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
using namespace std;
string rev(string s){
reverse(s.begin(),s.end());
return s;
}
string add(string s1,string s2){
;
string s=s1;
;i>=;i--){
'+carry;
s[i]=temp%+';
carry=temp/;
}
) s='+s;
return s;
}
bool ispali(string s){
int len=s.length();
;i<len;i++){
]){
return false;
}
}
return true;
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
string num,s;
cin>>num;
if(num==rev(num)){
cout<<num<<" is a palindromic number.";
;
}
;
bool flag=false;
while(k--){
s=rev(num);
string sum=add(s,num);
flag=ispali(sum);
cout<<num<<" + "<<s<<" = "<<sum<<endl;
if(flag==true){
cout<<sum<<" is a palindromic number.";
break;
}
num=sum;
}
){
cout<<"Not found in 10 iterations.";
}
;
}
1136 A Delayed Palindrome (20 分)的更多相关文章
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- PAT 1136 A Delayed Palindrome
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k ...
- pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分) Consider a positive integer N written in standard notation with k+1 ...
- 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 ...
- 1136 A Delayed Palindrome
题意:略. 思路:大整数相加,回文数判断.对首次输入的数也要判断其是否是回文数,故这里用do...while,而不用while. 代码: #include <iostream> #incl ...
- PAT1136:A Delayed Palindrome
1136. A Delayed Palindrome (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT_A1136#A Delayed Palindrome
Source: PAT_A1136 A Delayed Palindrome (20 分) Description: Consider a positive integer N written in ...
- PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
随机推荐
- Red Hat OpenShift
Core Concepts 基本概念 Master Master监控其他Node和Pod 提供Web Console Node Node是k8s的工作机器 Node中运行Pod,pod可以在node中 ...
- 《DSP using MATLAB》Problem 7.1
只有春节那么几天才能和家人团聚,看着爸爸妈妈一年比一年老,自己还是一无所有,照顾好自己尚且惭愧,真是悲从中来,又能怎么办呢, 唯有奋发努力,时不我待,多想想怎么赚钱,加油. 代码: function ...
- 【BZOJ3672】【UOJ#6】【NOI2014】随机数生成器
暴力出奇迹 原题: 2≤N,M≤5000 0≤Q≤50000 0≤a≤300 0≤b,c≤108 0≤x0<d≤108 1≤ui,vi≤N×M 恩首先容易看出来这个棋盘直接模拟搞出来就行了,不用 ...
- CSVN备份初体验
备份方法挺多的,目前我知道有四种 其一: 首先复制旧csvn服务器上repositories下的版本库文件夹到新csvn服务器repositories文件夹下面(做以下修改时最好把csvn服务停掉) ...
- awesome-workflow-engines
转自:https://github.com/meirwah/awesome-workflow-engines awesome-workflow-engines A curated list of aw ...
- sqler sql 转rest api 防止sql 注入
sqler 对于sql Sanitization 的处理,我们可以使用bind 指令 说明: 这个是2.0 的功能,注意版本的使用 参考格式 addpost { // $input is a ...
- out, ref 和 params 的区别和用法
1. out 参数. 如果你在一个方法中,返回多个相同类型的值,可以考虑返回一个数组. 但是,如果返回多个不同类型的值,返回数组就不可取.这个时候可以考虑使用out参数. out参数就侧重于在一个方法 ...
- RTSP为什么VLC播放器无法播放
rtsp_tracepoint: rtspservice.c,RTSP_state_machine, state_machine:current state is ready state curren ...
- Why aren't more desktop apps written with Qt?
Ref http://programmers.stackexchange.com/questions/88685/why-arent-more-desktop-apps-written-with-qt ...
- [转]总结@Autowired 和@Resource
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了. @Resource有两个属性是比较重要的,分 ...