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. .net 将DLL程序集生成到指定目录中

    .在程序集右键属性 .在程序集属性界面中找到生成事件 在预先生成事件命令行添加: IF NOT EXIST "$(ProjectDir)..\Bin" MD "$(Pro ...

  2. Objective-C官方文档 协议

    版权声明:原创作品,谢绝转载!否则将追究法律责任. 在现实生活中,当处理某一情况的时候人们往往遵循严格的程序.执法人员他们在打官司的收集证据和询问的时候一定要遵守协议. 在面向对象的语言中,最重要的是 ...

  3. 【python3】 django2.0 加载css 、js 、img 等静态文件

    python : 3.6.4 django : 2.0 1 文件目录  /var/www/mysite 2 nginx配置: server { listen ; #暴露给外部访问的端口 server_ ...

  4. 如何修改 VIM 制表符的空格数?

     想修改一下编辑器vi里的制表符(Tab)的空格数.因为它默认的太长(默认是8个空格).  在网上搜到了这篇文章http://my.oschina.net/captaintheron/blog/515 ...

  5. IMAP命令与分析

    https://www.cnblogs.com/crystalray/p/3304688.html Internet Mail Access Protocol(缩写为IMAP,以前称作交互邮件访问协议 ...

  6. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

  7. ps软件使用的问题解决记录

    1.PS的字体颜色改变不了的解决方法,添加字体的时颜色无论怎么选都只能有[黑.白.灰]三种颜色,     问题的原因:图像的模式选择了灰度(G)     解决方法:图像-->模式-->RG ...

  8. iOS学习笔记06—Category和Extension

    iOS学习笔记06—Category和Extension 一.概述 类别是一种为现有的类添加新方法的方式. 利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inher ...

  9. CentOS7.5安装Tomcat8

    一.tomcat的简介 这是Apache Tomcat Servlet / JSP容器的文档包的顶级入口点 .的Apache Tomcat 8.0版实现了Servlet 3.1和JavaServer ...

  10. Git之远程仓库

    1,注册账号 登录https://github.com注册一个账号 2,上传公钥 本地CentOS使用命令 ssh-keygen -t rsa生成秘钥 复制秘钥输入到github网站 3,新建仓库 4 ...