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. Kafka启动报错

    文章目录 问题 解决 问题 通过 ./kafka-server-start.sh ../config/server.properties 启动kafka 之前在server.properties中修改 ...

  2. composer 手动安装及简单使用 windows

    1.配置系统变量 Path 计算机->高级系统设置->环境变量->找到系统变量Path  双击 加入  ;php根目录地址:php中ext地址    如 :“;D:\phpStudy ...

  3. 抓取猫眼TOP100的数据

    import requests import re import json from multiprocessing import Pool from multiprocessing import M ...

  4. PHP上传文件超过文件最大限制导致无法上传成功

    最近在学习<HeadFirst PHP & MySQL>一书的第5章"使用存储在文件中的数据",做一个文件上传的应用时,出现了错误,就是文件无法成功上传.这个问 ...

  5. 神器,阿里巴巴Java代码检查插件

    背景 不久,又一气呵成发布了Eclipse/Intellij Idea下的代码检测插件PC3,可谓是国内代码优秀的检测插件.此插件检测的标准是根据<<阿里巴巴Java开发手册(终极版)&g ...

  6. uvloop官网翻译

    魔术堆栈 uvloop:快速的Python网络连接 作者Yury Selivanov @ 1st1 2016年5月3日 TL; DR asyncio是Python标准库附带的异步I / O框架.在此博 ...

  7. python 数据压缩

    zlib 压缩 import zlib import this s = this.s.encode('utf8')*10 for i in range(10): data = zlib.compres ...

  8. CF1205B

    CF1205B 由鸽巢原理n比较大的时候直接输出3 然后剩下的就可以跑最小环 #include<iostream> #include<cstdio> #include<c ...

  9. android中的原始资源的使用

    原始资源可以放在两个地方: 1.位于/res/raw目录下,android SDK会处理该目录下的原始资源,android SDK会在R清单类中为该目录下的资源生成一个索引项. 2.位于/assets ...

  10. 关于solr的一些知识

    简单了解 怎么理解Solr是个什么东西呢? 引用官网的介绍, Solr is the popular, blazing-fast, open source enterprise search plat ...