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 ...
随机推荐
- python os模块使用笔记(更新)
import os 添加os模块 walk方法: os.walk(path) path是string形式的目标目录 生成一个某目录下递归树形目录迭代器,方便递归访问子目录,访问目录就能够轻松访问子文件 ...
- 打开网页直接弹出qq对话框?
代码一: http://wpa.qq.com/msgrd?v=3&uin=此处输入QQ号&site=qq&menu=yes 代码二: <iframe src=" ...
- Intellij部署Tomcat问题
Intellij部署Tomcat问题 Warnings:No artifacts marked for development 切换到Development标签页 选择+按钮,并选择Artifacts ...
- maven项目eclipse目录结构浅析
maven项目eclipse目录结构浅析 PS:Java Resources是为了方便我们编译,到最后都会编译到 WEB-INF/classes Maven项目的目录结构
- C#中Socket关闭 Close、Dispose、Shutdown、Disconnect
An answer on StackOverflow made me think I have finally reached some glimpse of an understanding. Th ...
- EMC EMI 自行评估记录
EMC EMI 自行评估记录 设备 频谱仪 网房 评估 设置频谱频率为 30M 1G. 给频谱仪接一个天线,将被测试的机器上电然后在不同的角度换,看频谱仪上的数值. 可以绕上去,但不要和 PCB接触.
- 爬取ofo共享单车信息
前段时间看到很多微信公众号在转发一篇爬取mobike单车的信息,也不知道什么原因,在网上搜索了下很少有人在爬取ofo共享单车的数据,所以决定看看可以爬取ofo共享单车的那些数据. 抓取数据开始的时候, ...
- VMware Ubuntu如何连接互联网
Brigde——桥接 :默认使用VMnet0 1.原理: Bridge 桥”就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在”桥”上,运行着程序,让局域网A中的所有数据包原封不动的 ...
- 代理本地局域网其他PC上网的设置
- golang 指针在struct里的应用
type aa struct { b *int c string } func main() { var data int = 0 var ip *int /* 声明指针变量 */ ip = & ...