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(java)学习笔记95:Android原理揭秘系列之View、ViewGroup
作过Android 应用开发的朋友都知道,Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器, ...
- ASP.NET MVC and jqGrid 学习笔记 1-基本配置
新建一个mvc项目后
- uva 699 The Falling Leaves dfs实现
额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: ...
- Linux 源码安装apache 与常见错误解决
文档原位置 一.编译安装apache 1.解决依赖关系 httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级. 升级方式有两种,一种是通过源代码编译安装,一种是直接升 ...
- sendkeys用法详解
将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法 SendKeys string[, wait] SendKeys 语句的语法具有以下几个命名参数: 部分 描述 string ...
- Spring(3.2.3) - Beans(5): 集合属性的注入
如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...
- Linux 命令 - sort: 行排序文本文件
命令格式 sort [OPTION]... [FILE]... 命令参数 -b, --ignore-leading-blanks 忽略开头的空白字符. -d, --dictionary-order 只 ...
- Android手机播放电脑视频文件-屌丝必备
今天早上一到办公室,照常打开博客园看文章,看到有一片文章是用 http://www.cnblogs.com/wdfrog/p/3738180.html 看到这哥们实现的方法好复杂,又是配置电脑端,又 ...
- DOS批处理命令-SET命令
SET是专门用来创建.设置.查看或删除环境变量. 总结了下,SET的使用语法一般有下面几种 1.SET 变量名=变量值 这边有一点要注意的,就是变量名和变量值中间的等号两端不需要也不能有空格 看看下面 ...
- OpenMP多线程linux下的使用,简单化
http://hi.baidu.com/diwulechao/item/bc6d865c411b813c32e0a932 http://www.cnblogs.com/yangyangcv/archi ...