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

题目大意:求一个数是否是回文数,如果不是,则将与其反转数相加后,再次判断是否是回文数,以此类推。

解题:好几次没把握住数据长度,int-long-long long发现都不够,最后只能用字符型!无奈脸,从题目中推出10^10,进行100次叠加,数据可能是10^20,超过long long 的10^19;

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
string add(string s,string rev){
string s1;
int length = s.size();
int i;
int index = 0;
int sum;
for(i=length-1;i>=0;i--){
sum = s[i] + rev[i] - '0' - '0' +index;
s1.insert(s1.begin(),sum%10+'0');
index = sum/10;
}
if(index){
s1.insert(s1.begin(),index+'0');
}
return s1;
}
int palindromic(string& s,string& rev){
rev = s;
reverse(rev.begin(),rev.end());
if(s == rev){
return true;
}
s = add(s,rev);
return false;
}
int main(){
string s,rev;
int k;
cin>>s>>k;
int i=0;
while(i < k){
if(palindromic(s,rev)){
break;
}
i++;
}
cout<<s<<endl<<i<<endl;
return 0;
}

  

1024. Palindromic Number (25)的更多相关文章

  1. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  2. 1024 Palindromic Number (25)(25 point(s))

    problem A number that will be the same when it is written forwards or backwards is known as a Palind ...

  3. 【PAT】1024. Palindromic Number (25)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  4. PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]

    题目 A number that will be the same when it is written forwards or backwards is known as a Palindromic ...

  5. 1024 Palindromic Number (25 分)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  6. PAT (Advanced Level) 1024. Palindromic Number (25)

    手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...

  7. PAT甲题题解-1024. Palindromic Number (25)-大数运算

    大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...

  8. 【PAT甲级】1024 Palindromic Number (25 分)

    题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...

  9. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

随机推荐

  1. 【MOOC EXP】Linux内核分析实验七报告

    程涵  原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 可执行程序的装载 知识点梳理 一.预处 ...

  2. Leetcode 279. 完全平方数

    题目描述: https://leetcode-cn.com/problems/perfect-squares/ 解题思路: 同样是dp,一开始的想法是,对于每个数i做拆分为j和(i-j),利用动态转移 ...

  3. shell脚本--文件测试

    文件测试是指测试某一个文件或者目录是否存在 测试文件格式[ 操作符 目录或者文件 ]    注意左括号和操作符之间有一个空格,文件或者目录 与右边的括号之间也有一个空格. -d 测试是否为目录 -e ...

  4. the confilict between validation and readonly in jquery

    How can I disable jquery validation on readonly fields? http://stackoverflow.com/questions/10803294/ ...

  5. Oracle 和 SQLSERVER 重新获取统计信息的方法

    1. Oracle 重新获取统计信息的命令 exec dbms_stats.gather_schema_stats(ownname =>) # 需要修改 ownername options 指定 ...

  6. [转帖] 数据库用优化方案 https://segmentfault.com/a/1190000006158186

    Mysql大表优化方案     当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部 ...

  7. CSS 范围选择器(自编)

    选择第一个到第六个li元素ul li:nth-child(n+3):not(:nth-child(n+6)){} 选择第二个到最后一个ul li:nth-child(2)~li{} 选择除了第一个和最 ...

  8. [转]ubuntu中查找软件的安装位置

    原博客地址:http://www.cnblogs.com/zhuyatao/p/4060559.html ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get instal ...

  9. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 7. 条件循环

    条件循环能够让程序在条件成立时(即为真时)重复执行循环体中的语句.如果条件一直成立(即永远不会为假),则循环会一直进行下去,不会停止.如果初始时,条件不成立,则循环 1 次也不会执行.Python 中 ...

  10. ceph 性能测试

    我在物理机上创建了5台虚拟机,搭建了一个ceph集群,结构如图: 具体的安装步骤参考文档:http://docs.ceph.org.cn/start/ http://www.centoscn.com/ ...