Palindromic Number (还是大数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
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. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.
Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.
Output Specification:
For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.
Sample Input 1:
67 3
Sample Output 1:
484
2
Sample Input 2:
69 3
Sample Output 2:
1353
3
#include <iostream> #include <string> #include <algorithm> using namespace std; int aa1[]; int aa2[]; int main() { string n;int k; while(cin>>n) { cin>>k; int i,j,t; bool ifid=true; for(i=,j=n.length()-;i<=j;i++,j--) { if(n[i]!=n[j]) { ifid=false; break; } } if(ifid) { cout<<n<<endl; cout<<<<endl; } else { for(i=;i<;i++) { aa1[i]=; aa2[i]=; } int count=; for(i=n.length()-;i>=;i--) { aa1[count]=n[i]-''; aa2[count]=n[i]-''; count++; } reverse(aa2,aa2+count); int tem=; int sum=; for(i=;i<=k;i++) { for(j=;j<count;j++) aa1[j]=aa1[j]+aa2[j]; sum++; for(j=;j<count;j++) { if(aa1[j]>) { tem=aa1[j]/; aa1[j+]=aa1[j+]+tem; aa1[j]=aa1[j]%; } } if(aa1[j]!=) count++; bool ifis=true; for(j=,t=count-;j<=t;j++,t--) { if(aa1[j]!=aa1[t]) { ifis=false; break; } } if(ifis) { break; } else { for(j=;j<count;j++) aa2[j]=aa1[j]; reverse(aa2,aa2+count); } } for(j=count-;j>=;j--) cout<<aa1[j]; cout<<endl; cout<<sum<<endl; } } return ; }
Palindromic Number (还是大数)的更多相关文章
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- PAT A1024 Palindromic Number (25 分)——回文,大整数
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT 1024 Palindromic Number[难]
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PTA (Advanced Level) 1024 Palindromic Number
Palindromic Number A number that will be the same when it is written forwards or backwards is known ...
- 1024 Palindromic Number (25 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- General Palindromic Number (进制)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limit: 65536 KB A number that will ...
- PAT1019:General Palindromic Number
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
随机推荐
- android 编写动画
1.在编写动画的时候需要新建一个xml 新建的步骤是选中res单击右键选择Android resource file 然后弹出一个框 ,然后再Resource Type 里面选择Animation 然 ...
- 错误与修复:ASP.NET无法检测IE10,导致_doPostBack未定义JavaScript错误,恒处于FF5卷动条位置
浏览器版本号继续升级过程中.IE9诞生了,IE10 也即将问世,火狐5和6已经发布了,而7和8也快出现了,Opera已经到了11,Chrome还在继续,我也不知道,应该总在14和50之间吧.不管怎样, ...
- 关于修改Eclipse工作空间对应的文件夹名称之后的处理.
把文件夹名字从"xhkong"变成"xhkong(maintenance5.6)"之后打开这个工作空间. 导入git仓库我发现了一个之前没有发现的小技巧. 导入 ...
- 关于JDK中正则表达式
正则表达式的构造摘要 构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn ...
- hdu 3473 裸的划分树
思路: 用Sum[dep][i]记录从tree[po].l到i中进入左子树的和. #include<iostream> #include<algorithm> #include ...
- hdu3584 树状数组
思路:从一维扩展到三维.可以看看poj2155的解法. #include<iostream> #include<cstring> #include<algorithm&g ...
- poj 2723 2-SAT问题
思路:二分枚举能开的门的数量,将每次枚举转换成2-SAT问题.这里存在的矛盾是假设有门上a,b两个锁,a锁对应于1号钥匙,而一号钥匙的配对是2号钥匙,b锁对应于3号钥匙,3号的配对是4号钥匙.那么2号 ...
- Android中更新视图的函数onDraw()和dispatchdraw()函数的区别
Android的view组件显示主要经过mesure, layout和draw这三个过程.在mesure阶段里调用mesure(int widthSpec, int heightSpec)方法,这个方 ...
- 网页播放amr格式文件
mp3格式大家都知道,amr格式就陌生了,至少我没听过.百度一下,先给自己扫盲: AMR(Adaptive Multi-Rate):nokia为WB-AMR格式(AWB)的铃声所作的商业命名,以被3G ...
- ENVI/IDL与ArcGIS集成开发的三种途径
转载:本文来自ENVI5.0-IDL8.2系列产品白皮书_201303.PDF(Esri中国官网可下载)中P7-P10 ENVI 是一个非常开放的平台,提供一个健全的函数库,几乎涵盖ENVI 平台大部 ...