PAT 1136 A Delayed Palindrome
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.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; string reversestr(string s){
string res = "";
for(auto x:s){
res = x+res;
}
return res;
} string strplus(string a,string b){
string res = "";
int len = a.size();
int jinwei = ;
for(int i=len-;i >= ;i--){
int numa = a[i] - '';
int numb = b[i] - '';
int numsum = numa + numb + jinwei;
jinwei = numsum/;
int add = numsum%;
res = to_string(add) + res;
}
if(jinwei) res = ""+res;
return res;
} int main(){
string s;
cin >> s;
int cnt = ; while(){
string t = reversestr(s);
if(t == s) {
printf("%s is a palindromic number.", s.c_str());
break;
} string kkp = strplus(s,t); printf("%s + %s = %s\n",s.c_str(),t.c_str(),kkp.c_str()); s = kkp; cnt++;
if(cnt >= ){
printf("Not found in 10 iterations.");
break;
}
} return ;
}
大数相加突然变得好容易啊。。。
PAT 1136 A Delayed Palindrome的更多相关文章
- PAT 1136 A Delayed Palindrome[简单]
1136 A Delayed Palindrome (20 分) Consider a positive integer N written in standard notation with k+1 ...
- 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 (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- PAT A1136 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 (20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- 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 ...
- A1136. Delayed Palindrome
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
随机推荐
- PHP获取汉字首字母函数
<?php function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($str{0}); if ...
- 【Codeforces Round】 #431 (Div. 2) 题解
Codeforces Round #431 (Div. 2) A. Odds and Ends time limit per test 1 second memory limit per test ...
- Caravel–一款开源OLAP+数据可视化分析前端工具,支持Druid和Kylin
参考此文:http://lxw1234.com/archives/2016/06/681.htm
- 孙子兵法的计是最早的SWOT分析,《孙子兵法》首先不是战法,而是不战之法。首先不是战胜之法,而是不败之法
孙子兵法的计是最早的SWOT分析,<孙子兵法>首先不是战法,而是不战之法.首先不是战胜之法,而是不败之法 在打仗之前,你要详细地去算. 计算的目的是什么呢?孙子说,是为了知胜,就是为了知道 ...
- CCF CSP 201512-1 数位之和
题目链接:http://118.190.20.162/view.page?gpid=T37 问题描述 试题编号: 201512-1 试题名称: 数位之和 时间限制: 1.0s 内存限制: 256.0M ...
- 【mac微信小助手】WeChatPlugin使用教程!
微信小助手 mac版集微信防撤回和微信多开等诸多功能于一身,可以有效的阻止朋友微信撤回消息,还能开启无手机验证登录,再也不用每次登录扫码验证啦,非常方便! wechatplugin mac版安装教 ...
- 踩坑之路---JWT验证
使用JWT验证客户的携带的token 客户端在请求接口时,需要在request的head中携带一个token令牌 服务器拿到这个token解析获取用户资源,这里的资源是非重要的用户信息 目前我的理解, ...
- .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)——转载
原文链接:https://blog.walterlv.com/post/dotnet-high-performance-reflection-suggestions.html ***** 大家都说反射 ...
- 旧版本firefox添加扩展addons的地址
不要在 firefox 本身的addons 中去查找, 搜索, 那个是搜索不到的, 因为那个是针对 最新版的, 旧版本的很多插件都不能用, 被移除了, 要在 那个专门 提供 插件的站点中去寻找扩展 h ...
- [minecraft]mcCoder制作有感
mcCoder是一个minecraft-forge-mod制作库,力图让mod制作者可以更简单的制作mod,减少mod制作者的mod制作难度. 在GitHub上关注这个项目: 原理 mcCoder主要 ...