Source:

PAT A1024 Palindromic Number (25 分)

Description:

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

Keys:

Code:

 /*
Data: 2019-07-11 20:22:26
Problem: PAT_A1024#Palindromic Number
AC: 30:25 题目大意:
非回文数可以通过若干次逆置,相加的方法得到一个回文数;
现给一个数N和步数K,判断K步之内能否得到一个回文数
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std; string Add(string s1, string s2)
{
string s;
int carry=,len=s1.size();
for(int i=; i<len; i++)
{
int temp = (s1[len-i-]-'')+(s2[len-i-]-'')+carry;
s.insert(s.end(), (temp%)+'');
carry = temp/;
}
if(carry != )
s.insert(s.end(), carry+'');
reverse(s.begin(),s.end());
return s;
} bool IsPalin(string s)
{
for(int i=; i<s.size(); i++)
if(s[i] != s[s.size()-i-])
return false;
return true;
} int main()
{
int k,i;
string s,t;
cin >> s >> k;
for(i=; i<k; i++)
{
if(IsPalin(s))
break;
t = s;
reverse(t.begin(), t.end());
s = Add(s,t);
}
cout << s;
printf("\n%d",i); return ;
}

PAT_A1024#Palindromic Number的更多相关文章

  1. General Palindromic Number (进制)

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

  2. Palindromic Number (还是大数)

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

  3. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  4. PAT1019:General Palindromic Number

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

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

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

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

  8. A1019. General Palindromic Number

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

  9. A1024. Palindromic Number

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

随机推荐

  1. 数据库SQL调优的几种方式(转)

    原文地址:https://blog.csdn.net/u010520146/article/details/81161762 在项目中,SQL的调优对项目的性能来讲至关重要,所有掌握常见的SQL调优方 ...

  2. 分析/proc/[pid]/maps中的各个内存区域的大小

    cat maps | sed -e "s/\([0-9a-f]\{8\}\)-\([0-9a-f]\{8\}\)/0x\1 0x\2/" | awk '{printf(" ...

  3. VIP视频下载终结器

    youtube-dl: Youtube-dl是谷歌github上的一个开源项目,它是一款轻量级的命令行 下载实用工具,阿刚曾在乐软博客里文章<不仅仅是youtube,youtube-dl在线视频 ...

  4. os.walk|图片数据集

    该函数的功能:遍历指定文件夹下的所有[路径][文件夹][文件名] ''' os.walk(top[, topdown=True[, onerror=None[, followlinks=False]] ...

  5. os.fork()----linux

    fork() 函数,它也属于一个内建并 且只在 Linux 系统下存在. 它非常特殊普通的函数调用,一次返 回但是 fork() 调用一次,返回两次.因为操作系统自动把当前进程(称为父)复制了一份(称 ...

  6. Eureka 系列(07)服务注册与主动下线

    Eureka 系列(07)服务注册与主动下线 [TOC] Spring Cloud 系列目录 - Eureka 篇 在上一篇 Eureka 系列(05)消息广播 中对 Eureka 消息广播的源码进行 ...

  7. oracle数据库 唯一约束的创建与删除

    1.创建索引: alter table TVEHICLE add constraint CHECK_ONLY unique (CNUMBERPLATE, CVIN, CPLATETYPE, DWQCH ...

  8. LNMP一键安装包+Thinkphp搭建基于pathinfo模式的路由

    LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat/Fedora.Debian/Ubuntu/Raspbian/Deepin VPS或独立主机安装LNMP(Ngin ...

  9. CF986C

    CF986C 给\(A_i\)连一条向补集的边和子集的边,然后dfs求联通块数 #include<iostream> #include<cstring> #include< ...

  10. mybatis 教程(mybatis in action)

    目录简介: 一:开发环境搭建二:以接口的方式编程 三:实现数据的增删改查 四:实现关联数据的查询 五:与spring3集成(附源码) 六:与Spring MVC 的集成 七:实现mybatis分页(源 ...