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. 当前系统的CPU和内存的空闲百分比

    设想我们有一个php页面A比较耗资源,因此在每次执行页面A中的代码前需要检测一下系统目前CPU和内存的空闲百分比.我们可以利用下面几个函数来解决这个问题 1 2 3 4 5 6 7 8 9 10 11 ...

  2. spring 数据库字段映射

    当有复杂名称字段时: 在repository中写代码字段名 List<Grid> findByLocIsWithin(GeoJsonPolygon boundary); 可以添加field ...

  3. spring boot starter开发

    作为公司的技术保障部,一直承担着技术方向的把控,最近公司准备全面转入spring boot的开发.所以我们部门也一直在调研相关的技术知识点: 使用springboot开发应用已经有一段时间了,我们都沉 ...

  4. Struts1.3——文件上传和下载

    1.Struts文件上传 在Web开发中,会经常涉及到文件的上传和下载,比如在注册账户的时候,我们需要上传自己的头像等. 我们可以利用Struts很方便地实现文件的上传. 1.1 开发步骤 现在,假设 ...

  5. Jenkins 搭建篇

    1.Jenkins 介绍 自动化运维工具:saltstack.jenkins.等.因为他们的目标一样,为了我们的软件.构建.测试.发布更加的敏捷.频繁.可靠 如果运维对git不熟,是无法做自动化部署. ...

  6. postgresql like 中的转义

    select * from tb_org where char_length(xdm)>8 and xdm not like '%*_%'  ESCAPE '*' ESCAPE 后面的 * 是转 ...

  7. 统计List中相同的元素

    public static <E> List<E> getCommonsElements(List<E> list) { return list.stream() ...

  8. VirtualBox安装CentOS系统

    1. 准备材料 虚拟机软件: VirtualBox 系统iso版本:CentOS-7-x86_64-DVD-1611.iso 虚拟机软件下载地址: https://www.virtualbox.org ...

  9. day03 python数据类型 数字 字符串 布尔

    day03 python   一.基本数据类型     1.int a= 8 a_length = a.bit_length()  #此方法求数字的二进制长度  print(a_length)     ...

  10. vue子组件获取父组件方法

    注:以下代码未使用esLint语法检查 父组件: <template> <div class="wrapper"> <cp_action @paren ...