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 (<= 10^10^) 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
题目大意就是:给出一个大数,并将其反转,在有限次k次相加内,若结果是回文数,那么就输出并且输出计算次数;否则就直接输出计算k次的结果。
注意到n可能会非常大,10的10次方,而且要进行k次(最多100次),所以用long long不可以了,用string来做大数运算,并且string有reverse函数。
还有主函数中的逻辑判断问题,因为一个数如果是回文数,那么运算结果肯定也是回文数,只要判断一个数反转后是否等于自身,如果相等,那么
就已经找到回文数,就break即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
//
string add(string s1,string s2){
long long len=s1.size();
int a,b=;
for(int i=;i<len;i++){
if(b==)
a=(s1[i]-'')+(s2[i]-'');
else{
a=(s1[i]-'')+(s2[i]-'')+;
b=;
}
if(a>=){
b=;
a%=;
}
s2[i]=a+'';
}
if(b==){
s2=s2+'';
}
reverse(s2.begin(),s2.end());
return s2;
}
bool isH(string s){
long long len=s.size();
long long f=len/;
bool flag=true;
for(int i=;i<f;i++)
if(s[i]!=s[len-i-]){
flag=false;break;
}
return flag;
}
int main()
{
string s;
int k;
cin>>s>>k;
string s2;
int n=;
bool flag=false;
while(n<k){
s2=s;
reverse(s.begin(),s.end());
if(s2==s){
break;
}else{
s=add(s,s2);
n++;
}
}
cout<<s<<'\n'<<n;
return ;
}
 

PAT 1024 Palindromic Number[难]的更多相关文章

  1. PAT 1024 Palindromic Number

    #include <cstdio> #include <iostream> #include <cstdlib> #include <algorithm> ...

  2. PAT 甲级 1024 Palindromic Number

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

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

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

  4. 【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 ...

  5. 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 ...

  6. 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 ...

  7. 1024 Palindromic Number int_string转换 大整数相加

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

  8. 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 ...

  9. 1024. Palindromic Number (25)

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

随机推荐

  1. 【面试题】源石智影科技Python工程师笔试题

    哈哈 上图

  2. python基础---->python的使用(三)

    今天是2017-05-03,这里记录一些python的基础使用方法.世上存在着不能流泪的悲哀,这种悲哀无法向人解释,即使解释人家也不会理解.它永远一成不变,如无风夜晚的雪花静静沉积在心底. Pytho ...

  3. echarts - 树图实现四个层级

    我相信很多人和我一样,制作echats图标时,都会先去demo官网找相同的或者近似的效果,然后再此基础上改进成我们想要的那个. 但是近期混迹某微信群时,我看到一个群友抛出问题说,echarts画树状图 ...

  4. LeetCode - 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  5. python之Tkinter控件学习

    转载自  http://www.cnblogs.com/kaituorensheng/p/3287652.html#_label0 阅读目录 1. 产品介绍 2. 设计规划 3. 相关知识 4. 源码 ...

  6. CF 1073C Vasya and Robot(二分答案)

    C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Junit4单元测试报错

    转载博客:http://www.cnblogs.com/sxdcgaq8080/p/5649819.html 今天是用JUnit测试一段代码,报错method initializationerror ...

  8. DragonBones龙骨发布后在Egret中的位置

    DragonBones发布后的动画,加载到Egret中场景中,原点的位置在哪呢? DragonBones中的图片位置 导出 加载到Egret中.可见DragonBones中的图片位置原点左下方(0,0 ...

  9. 新浪的动态策略灰度发布系统:ABTestingGateway

    原文链接:http://www.open-open.com/lib/view/open1439889185239.html ABTesingGateway 是一个可以动态设置分流策略的灰度发布系统,工 ...

  10. Pyqt中富文本编辑器

    对于文本编辑,qt提供了很多控件 QLineEdit:单行文本输入,比如用户名密码等简单的较短的或者具有单一特征的字符串内容输入.使用text.settext读写 QTextEdit:富文本编辑器,支 ...