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
 #include <iostream>
#include <string>
using namespace std;
int main()
{
string N, str;
int K, i = ;
cin >> N >> K;
if (N == str.assign(N.rbegin(), N.rend()))
K = ;
for (i = ; i <= K; ++i)
{
str = "";
int s = ;
for (int j = ; j < N.length(); ++j)
{
str += (s + N[j] - '' + N[N.length() - j - ] - '') % + '';
s = (s + N[j] - '' + N[N.length() - j - ] - '') / ;
}
if (s > )
str += s + '';
N.assign(str.rbegin(), str.rend());
if (N == str)
break;
}
cout << N << endl;
cout << (i > K ? K : i) << endl;
return ;
}

PAT甲级——A1024 Palindromic Number的更多相关文章

  1. PAT 甲级 1024 Palindromic Number

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

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

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

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

  4. PAT A1019 General Palindromic Number (20 分)——回文,进制转换

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

  5. A1024. Palindromic Number

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

  6. PAT 1019 General Palindromic Number

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  7. PAT 1019 General Palindromic Number[简单]

    1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...

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

  9. [PAT] A1019 General Palindromic Number

    [题目] https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 题目大意:给定一个N和b,求N在b ...

随机推荐

  1. 云-腾讯云-云通信:云通信(IM)

    ylbtech-云-腾讯云-云通信:云通信(IM) 企业数字化转型的通信助手,让通信触达全球,智联万物 1.返回顶部 1. 云通信(Instant Messaging,IM)承载亿级 QQ 用户即时通 ...

  2. iOS之SceneKit.h文件简介

    1.SceneKit简介 SceneKit(SK)是WWDC12推出的OS X平台的Cocos 3D渲染引擎框架.支持粒子效果,物理模拟,脚本事件,多程渲染,支持iOS平台.SceneKit整合了Co ...

  3. Google Chrome浏览器安装xpath helper插件

    1. 图中桌面的两个2.0.2_0文件就是xpath helper插件. --------------------------------------------------------------- ...

  4. java开发系列-服务器tomcat

    概述 tomcat是Apache组织提供的,中小型的免费的支持Servlet和JSP规范的服务器. tomcat安装配置 下载 官方下载地址 下载,绿色版直接解压到指定目录就好. 启动Tomcat 双 ...

  5. 2019-9-2-贡献自己的服务器搭建tor中转

    title author date CreateTime categories 贡献自己的服务器搭建tor中转 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 ...

  6. CNN网络中的不变性理解

    神经网络中的不变性 原文:https://blog.csdn.net/voxel_grid/article/details/79275637     个人认为cnn中conv层对应的是“等变性”(Eq ...

  7. 16进制与utf-8

    很多人将数据的存储.传输方式和展现形式混为一谈. 类似的16进制 2进制是讲内容在电脑里面的存储或者传输的一种格式, 而utf-8 gb2312 等是输出的展现的一种格式 不是一回事,另外 gbk包含 ...

  8. Python全栈开发:web框架之tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

  9. JAVA-第一课 环境的配置

    首先 我们需要 下载java的开发工具包 jdk  jdk 的下载地址::http://www.oracle.com/technetwork/java/javase/downloads/index.h ...

  10. RSA算法的基本原理

    记得在我上初一的时候做过这么一道数学竞赛题,就是求7的222次方的个位数字.当时教材上介绍的解题方法是将222分解成4*55+2,然后算出7的2次方个个位数字即为要算的数值.当时年幼无知的我根本不了解 ...