1024. Palindromic Number (25)
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)的更多相关文章
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- 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 ...
- 【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 ...
- 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 ...
- 1024 Palindromic Number (25 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) 1024. Palindromic Number (25)
手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
- 【PAT甲级】1024 Palindromic Number (25 分)
题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
随机推荐
- 《Gogoing》Alpha版会议总结
一.开会的过程 首先大家对自己的任务进行了汇报,然后大家就当前最需要解决的问题提出解决方案,最后相互鼓励,相互帮助,探讨下一步该怎么做. 二.讨论的问题 百度地图API代码和界面代码为什么对接不上? ...
- C++中struct 和 class的区别
首先,C++中类的定义,从狭义上理解,就是我们使用的class类型.从广义上,类就是定义了一个新的类型和新的作用域,它具有成员函数和成员数据. 而对广义类定义的实现分为两种,一种是使用struct实现 ...
- eclipse新建maven项目,修改默认jdk版本
我们新建maven项目,默认jdk版本是1.5,我们可以修改成我们想要的版本,有两种方法 1每个项目修改,在pom.xml添加 <build> <plugins> <!- ...
- Event事件2
1,阻止默认行为: 2,事件监听 3,事件流之事件捕获 4,事件委托 阻止默认行为 浏览器中有很多默认行为,比如当点击a标签后,会发生链接的跳转.当点击鼠标的右键时, 会显示右键菜单等. 有些时候,浏 ...
- 关于miniconda的安装,配置以及包批量安装和使用
由于时间很晚了.就不写废话了. conda官方文档地址:http://conda.pydata.org/docs/ 一切其实都可以从miniconda的文档找到,这里只纪录自己操作的时候遇到的值得一说 ...
- notepad编写html
notepad写代码的过程: 1.打开notepad,新建一个文档,然后保存,文件的后缀名为.html,代码保存前界面上文件名称为红色,保存后蓝色: 2.保存为html后,进行代码的输入,如果设置好自 ...
- 面向对象基础及UML建模语言
1.面向对象的方法起源于面向对象程序设计语言,其发展过程大体经历了初始阶段.发展阶段和成熟阶段. 2.面向对象方法主要优点 (1)从认识论的角度可以看出,面向对象方法改变了开发软件的方式. (2)面向 ...
- maven编译的时候跳过test
方式1:用命令带上参数 mvn install -Dmaven.test.skip=true 方式2:在pom.xml里面配置 <plugin> <groupId>org.ap ...
- BZOJ3456 城市规划 【生成函数】【FFT】
题目分析: 容易想到生成函数的构造方法. 令g(n)表示n个点的无向图个数,f(n)表示n个点的无向连通图的个数.式子是显然的. 容易发现式子是卷积的形式,写出生成函数,然后多项式求逆后多项式乘法即可 ...
- Minimum Cost POJ - 2516 (模板题 spfa最小费用最大流)
题意: 人回家,一步一块钱,有x个人,y个房子,求能回家的最大人数且使之费用最小 解析: 就是....套模板,,,, 建图(⊙﹏⊙)...要仔细观察呐 对于人拆不拆都可以 都能过,,,,这里贴上拆开 ...