题目

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 nonpalindromic 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 afer 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

题目分析

给出一个整数N(<=10^10),最大操作次数K(<=100),若整数不为回文数,进行如下操作(1. 反转N得到FN;2.N=FN+N),循环执行,直到操作次数达到K次或者N成为回文数

  1. N若为最大值1010操作100次大于1020,超过long long类型范围,需要看做大整数处理数据(string)

解题思路

  1. 判断N是否为回文数,如果不是回文数,进入循环操作,直到操作次数达到k或者N成为回文数
  2. 大整数处理,需要逆置字符串,因为操作从数字的个位开始(但是a,b互为反转,所以本题中只需要反转一个就可以)

Code

Code 01

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
bool isPac(string & s) {
for(int i=0; i<s.length(); i++) {
if(i>=s.length()-1-i)break;
if(s[i]!=s[s.length()-1-i])return false;
}
return true;
}
string bignadd(string a) {
// b是a的反转 长度一定相等,无需考虑不相等的情况
string b = a;
reverse(b.begin(),b.end());
int carry=0;
string c;
for(int i=0; i<a.length(); i++) {
int tempa = a[i]-'0',tempb = b[i]-'0';
int temp = tempa+tempb+carry;
c.push_back(temp%10+'0');
carry=temp/10;
}
if(carry==1)c.push_back('1');
reverse(c.begin(),c.end());
return c;
}
int main(int argc,char *argv[]) {
string n,temp;
int m;
cin>>n>>m;
temp = n;
int i=1;
for(i=1; i<=m&&!isPac(temp); i++) {
temp = bignadd(temp);
}
printf("%s\n",temp.c_str());
printf("%d",i-1);
return 0;
}

最新文章

  1. Qt &amp;QSS
  2. JS数组求最大值和最小值
  3. sql server 跟踪各事件的字段项编码及解释
  4. C++ new失败的处理
  5. LeetCode Graph Valid Tree
  6. ChartControl
  7. linux 多线程信号处理总结
  8. 安卓 NEXUS6 修改分辨率,density
  9. springboot注解
  10. org.hibernate.PersistentObjectException: detached entity passed to persist异常
  11. Apache Hadoop压缩包与Eclipse结合,导入jar包归整总结(手动)
  12. MVC下载Excel
  13. 数据结构与算法分析 3.4&amp;3.5 — 链表的交与并算法
  14. bitnami redmine 安装插件
  15. Linux-进程描述(3)之进程状态僵尸进程与孤儿进程
  16. jQueryUI Autocomplete插件使用入门教程(最新版)---------转载
  17. Mybatis源码之SimpleExecutor
  18. T-SQL实用查询之查询字段所属的数据库表
  19. python:a+=b 和a=a+b? 基础数据类型也不能乱用
  20. python编写producer、consumer

热门文章

  1. 四十七、在SAP中,把功能区块整合成一个函数,通过调用函数的办法使代码简洁明了
  2. 三十八、SAP设置默认语言
  3. 面向对象第一个特征-封装(Encapsulation)
  4. P 1015 德才论
  5. sql拆分列 时间拆分 日、月、年
  6. 吴裕雄--天生自然TensorFlow2教程:高阶操作
  7. tensorflow中的神经网络笔记
  8. NoSQL:
  9. 关于Java中内省的总结
  10. textField 基本属性