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. IOS 基于APNS消息推(JAVA后台)

    直接上Demo import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUti ...

  2. Oracle查询最近执行过的SQL语句

    oracle 查询最近执行过的 SQL语句 select sql_text,last_load_time from v$sql order by last_load_time desc; SELECT ...

  3. mysql 5.7.20 取得动态sql执行结果

    drop procedure test; delimiter ;; CREATE procedure test() -- 取动态sql的值 begin ); ); set v_sqlcounts = ...

  4. Python面试题之下面代码会输出什么

    def f(x,l=[]): for i in range(x): l.append(i*i) print l f(2) f(3,[3,2,1]) f(3) 答案: [0, 1] [3, 2, 1, ...

  5. 38-python基础-python3-检查字典中是否存在键或值

    in 和 not in 操作符   请注意, 在前面的例子中,‘name’ in spam 本质上是一个简写版本.相当于'name' in spam.keys()

  6. Manjaro配置中国源

    1.自动寻找中国源 sudo pacman-mirrors -i -c China -m rank//更新镜像排名sudo vim /etc/pacman.d/mirrorlist //查看选择的源s ...

  7. cd 切换

    切换

  8. Dubbox服务的消费方开发

    开发步骤: (1)创建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依赖 ,同“dubboxdemo-service”工程.区别就是把tomcat插件的运行端口改为808 ...

  9. js实现点击按钮控制展开与收起.

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  10. 项目实战 - 混合式App开发

    为何要使用混合式开发? 要说为什么使用Hybrid App [混合式开发],就要先了解什么是Native App[原生程序], Web App[网站程序]. Native App 是专门针对某一类移动 ...