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. 一种新型聚类算法(Clustering by fast search and find of density peaksd)

    最近在学习论文的时候发现了在science上发表的关于新型的基于密度的聚类算法 Kmean算法有很多不足的地方,比如k值的确定,初始结点选择,而且还不能检测费球面类别的数据分布,对于第二个问题,提出了 ...

  2. Win 7打开任务管理器的几种方法

    1. 按住Ctrl和Alt键和Delete键 2. 快速启动栏打开win7任务管理器 3. Ctrl键+Shift键+Esc键的组合键 4. 桌面新建一个文本文档也叫记事本,打开,输入“C:\Wind ...

  3. Git 创建两个“本地分支”协同工作

    一 代码拉下来后,首先创建两个本地分之 $repo start master . //仅仅用于同步服务器的修改(此处master名字可以随意定,但是建议定成这样,好记忆) $repo start wo ...

  4. 设置RabbitMQ远程ip登录

    由于账号guest具有所有的操作权限,并且又是默认账号,出于安全因素的考虑,guest用户只能通过localhost登陆使用,并建议修改guest用户的密码以及新建其他账号管理使用rabbitmq. ...

  5. java.lang.NoClassDefFoundError: Could not initialize class xxx 原因

    一.问题及原因 程序里有个工具类,主要是调用它的静态方法来发送mq. 调用场景如下: 结果这两天报了个错: java.lang.NoClassDefFoundError: Could not init ...

  6. nginx 启动重启脚本

    #! /bin/sh # Default-Start:     2 3 4 5 # Default-Stop:      0 1 6 # Short-Description: starts the n ...

  7. 2015.7.7js-07-2(基础)

    1.用求模来计算时间,秒%60,就能获取剩余的秒 var s = 362; var minute = parseInt(s/60) + "分" //取得分 var second = ...

  8. vi 撤销操作

    'u' : 撤销上一个编辑操作 'ctrl + r' : 恢复,即回退前一个命令 'U' : 行撤销,撤销所有在前一个编辑行上的操作

  9. redmine3.3.0安装问题

    1.An error occurred while installing rmagick (2.16.0), and Bundler cannot continue. Make sure that ` ...

  10. VS2003安装Opencv1.0 windows系统 win7

    一.步骤 下载安装opencv1.0     安装文件我上传到百度网盘分享连接 http://pan.baidu.com/s/1o8na0aA 配置电脑windows环境变量 配置VS2003全局设置 ...