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 ...
随机推荐
- jquery中ajax向action传递对象参数,json ,spring注入对象
首先,我这个程序的框架是spring+struts2+hibernate. 后端的action的需要接受从前端传进来的参数,由spring的注入,可知,如果前端用的是form的话,只需要在每个inpu ...
- [Android]应用的前后台运行
在开发中,你是不是没有抽象一个出常用的类,那你可能要为你的懒惰付出很大的代价.要时刻记得自己的工具箱,不断往里面添加一些小小玩意.今天就给大家带来一个很有意思的例子.前后台运行!! 在Android开 ...
- python(1) - 条件判断和循环
条件判断: 比如输入用户年龄,根据年龄打印不同的内容,在python中用if语句实现: age = 20 if age >= 18: print('你可以看这个视频!') 可以给if加一个els ...
- hdu 4421 2-SAT问题
思路:我们需要判断是否有满足的a[n],其实也就是对每一个二进制位进行判断,看是否有满足的.那么我们每次取出一个二进制位,这样每一位只有0,1两种状态,就成了比较典型的2-SAT问题了. #inclu ...
- centos 安装 acrobat Reader之后
IV: 为Firefox等浏览器安装Acrobat Reader插件:sudo /usr/local/Adobe/Acrobat7.0/Browser/install_browser_plugin按照 ...
- 纯CSS3制作进度条源代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Servlet & JSP - Form-based Authentication
基本认证和摘要认证都只能使用浏览器自带的登录框而不能使用自定义的登录页面.如果必须使用自定义的登录页面,则可以选择基于表框的认证方式. 基于表框的认证的配置与基本认证和摘要认证的差别在于部署描述符中 ...
- 使用Google Code和客户端TortoiseSVN 工具搭建一个在线源代码版本控制系统
把代码放在Google Code里,客户端还是使用TortoiseSVN ,就可以很方便地在家里和办公室协调工作了,不用再用U盘把代码拷来拷去了. 搭建过程: 1.注册一个google账户:https ...
- Cocos2d-x中自定义粒子系统
除了使用Cocos2d-x的11种内置粒子系统外,我们还可以通过创建ParticleSystemQuad对象,并设置属性实现自定义粒子系统,通过这种方式完全可以实现我们说需要的各种效果的粒子系统.使用 ...
- javascript笔记——jsonp
上篇博客介绍了同源策略和跨域访问概念,其中提到跨域常用的基本方式:JSONP和CORS. 那这篇博客就介绍JSONP方式. JSONP原理 在同源策略下,在某个服务器下的页面是无法获取到该 ...