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 (≤) is the initial numer and K (≤) 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
题目分析:大数加法 把是否进1记录就好了
 #include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
string Add(string s1, string s2)
{
int flag = ;
int len = s1.length();
for (int i = len - ; i >= ; i--){
int temp = flag+s1[i] - '' + s2[i] - '';
if (temp > ){
s1[i] = (temp % ) + '';
flag = ;
}
else {
s1[i] = temp + '';
flag = ;
}
}
if (flag)return '' + s1;
else
return s1;
}
int main()
{
string N;
int K;
cin >> N >> K;
int i = ;
for (; i < K; i++){
string s =N;
reverse(s.begin(), s.end());
if (s == N)
break;
else
N = Add(N, s);
}
cout << N << endl << i;
}

最新文章

  1. Oracle学习笔记三 SQL命令
  2. KMP算法实现
  3. Azure SQL Database (21) 将整张表都迁移到Azure Stretch Database里
  4. 山东省滕州市木石镇化石沟村QQ群116528924
  5. WPF开发经验
  6. [转载]iOS Provisioning Profile(Certificate)与Code Signing详解
  7. Atitit &#160;记录方法调用参数上下文arguments
  8. Eclipse CDT Linux下内存分析 实战历险
  9. [ACM_水题] ZOJ 3712 [Hard to Play 300 100 50 最大最小]
  10. http cookies
  11. cisco路由基于策略的路由选择
  12. 基于Python+协程+多进程的通用弱密码扫描器
  13. codeforces 212E IT Restaurants(树形dp+背包思想)
  14. 反对抄袭 正解spring的@Autowired 不要相信网上的错误版本
  15. 仅当使用了列的列表并且 IDENTITY_INSERT 为 ON 时,才能为表&#39;SpeType&#39;中的标识列指定显式值
  16. C++/C#结构体转化-二维数组-bytes To Strings
  17. tolower (Function)
  18. Java中关于OOM的场景及解决方法(转)
  19. NYOJ353 3D dungeon 【BFS】
  20. 读书笔记—CLR via C#同步构造28-29章节

热门文章

  1. django 从零开始 11 根据时间戳加密数据
  2. Java集合01——List 的几个实现类,了解一下?
  3. C语言程序设计(十一) 指针和数组
  4. kafka实现无消息丢失与精确一次语义(exactly once)处理
  5. Python只有文件不存在才能写文件
  6. 安卓手机tcpdump的使用
  7. 【WPF学习】第五十九章 理解控件模板
  8. (转)协议森林05 我尽力 (IP协议详解)
  9. git常用命令学习配详细说明
  10. 五分钟学后端技术:如何学习Redis、memcache等常用缓存技术