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 ...
随机推荐
- CAML query for Group by count and data
CAML query for Group by count and data Company Category Product Name Microsoft Developer Visual Stud ...
- ASP.NET MVC and jqGrid 学习笔记 1-基本配置
新建一个mvc项目后
- AES 加解密
// // NSData+AES.h // Smile // // Created by 蒲晓涛 on 12-11-24. // Copyright (c) 2012年 BOX. All rights ...
- Curl上传文件
curl -v -XPOST -H /thumbnail
- html中input type=file 改变样式
<style> #uploadImg{ font-size:12px; overflow:hidden; position:absolute} #file{ position:absolu ...
- windows进程的创建方法
1.WinExec(LPCSTR lpCmdLine,UINT uCmdShow) >>参数: lpCmdLine:指定程序的相对路径或绝对路径,命令行参数 uCmdShow:指定窗口的显 ...
- mysql输出的错误提示是法语
参考MySQL用户手册 http://dev.mysql.com/doc/refman/5.5/en/error-message-language.html mysql.ini 文件里头有一个参数叫做 ...
- 理解Android系统的进程间通信原理(一)----RPC中的代理模式
Android系统中的进程间通信是通过一个轻量级的RPC(Remote Procedure Call远程进程调用)和AIDL(Android Interface Definination Langua ...
- 【.net】创建属于自己的log组件——改进版
在上一篇随笔中,建立了一个自己的Log简单日志记录类 可是在众多园友的提点下,对于线程,阻塞,资源竞争等都没有仔细的去了解 在这版的改进中,我们新加了线程操作,线程等待,以及多层的错误捕获.[不知 ...
- webkit,HTML5头部标签
大家都知道在移动前端开发中添加一些webkit专属的HTML5头部标签,帮助浏览器更好解析html代码,更好地将移动web前端页面表现出来.本文整理一些HTML5头部<meta>标签常用的 ...