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 (≤10​10​​) is the initial numer and K (≤100) 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 <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
long long tonum(string s) {
long long res = ;
for (int i = ; i < s.length(); i++) {
res = res * + s[i] - '';
}
return res;
}
string tos(long long num) {
string res = "";
do {
res += '' + num % ;
num /= ;
} while (num != );
reverse(res.begin(), res.end());
return res;
}
string add(string s1, string s2) {
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
string s3="";
int carry = ;
int i = ;
for (i; i < s1.length() && i < s2.length(); i++) {
int tmp1 = s1[i] - '';
int tmp2 = s2[i] - '';
int tmp = tmp1 + tmp2 + carry;
s3 += tmp % + '';
carry = tmp / ;
}
if (s1.length() > s2.length()) {
while(i<s1.length()){
if (carry != ) {
int tmp1 = s1[i] - '';
tmp1 += carry;
s3 += tmp1 % + '';
carry = tmp1 / ;
i++;
}
else break;
}
if (carry != ) s3 += '' + carry;
else if (i < s1.length()) {
s3 += s1.substr(i, s1.length() - i);
}
}
else if (s1.length() < s2.length()) {
while (i < s2.length()) {
if (carry != ) {
int tmp1 = s2[i] - '';
tmp1 += carry;
s3 += tmp1 % + '';
carry = tmp1 / ;
i++;
}
else break;
}
if (carry != ) s3 += '' + carry;
else if (i < s2.length()) {
s3 += s2.substr(i, s2.length() - i);
}
}
else if (s1.length() == s2.length()) {
if (carry != ) s3 += '' + carry;
}
reverse(s3.begin(), s3.end()); return s3;
}
bool pali(string s) {
string s2, s3;
s2 = s;
s3 = s;
reverse(s2.begin(), s2.end());
if (s2 == s3)return true;
else return false;
}
int main() {
string s;
int k;
cin >> s >> k;
int j = ;
string s_2;
s_2 = s;
if (!pali(s)) {
for (j = ; j <= k; j++) {
s_2 = s;
reverse(s_2.begin(), s_2.end());
s = add(s, s_2);
if (pali(s))break;
}
}
if (j == k + )j--;
cout << s << endl << j;
system("pause");
}

注意点:又是回文判断题,注意数字可能会超出long long 范围,所以只能用大数相加即两个字符串相加来做,回文的判断直接用了字符串的反转和==。

ps:发现字符串相加想太多了,一个数和他的反转两个数字一定位数是相等的,不用这么麻烦。而且做大整数加法正确的方式是给短的那个数补0,这样直接最后判断一下进位就好了。

PAT A1024 Palindromic Number (25 分)——回文,大整数的更多相关文章

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

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

  2. 【PAT甲级】1024 Palindromic Number (25 分)

    题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...

  3. 1024 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 (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

  5. JAVA 基础编程练习题25 【程序 25 求回文数】

    25 [程序 25 求回文数] 题目:一个 5 位数,判断它是不是回文数.即 12321 是回文数,个位与万位相同,十位与千位相同. package cskaoyan; public class cs ...

  6. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  7. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  8. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  9. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)

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

随机推荐

  1. 【Spring】23、ApplicationContext ,ApplicationContextAware,Listener,Event 的关系解读

    tomcat容器启动流程 启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是C ...

  2. 设计模式之备忘录模式(Memento )

    当我们在实际应用中需要提供撤销机制,当一个对象可能需要再后续操作中恢复其内部状态时,就需要使用备忘录模式.其本质就是对象的序列化和反序列化的过程,支持回滚操作. 作用 在不破坏封装性的前提下,捕获一个 ...

  3. Python shelve

    shelve模块只有一个open函数,返回类似字典的对象,可读可写; key必须为字符串,而值可以是python所支持的数据类型. import shelve f = shelve.open('SHE ...

  4. Python 练习:三级菜单选择城市

    info = { 'GuangDong':{ 'GuangZhou': ['TianHe', 'HaiZhu'], 'MaoMing': ['MaoNan', 'DianBai']}, 'ShanDo ...

  5. Expo大作战(三十四)--expo sdk api之LinearGradient(线性渐变),KeepAwake(保持屏幕不休眠),IntentLauncherAndroid,Gyroscope,

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  6. JavaScript大杂烩7 - 理解内置集合

    JavaScript内置了很多对象,简单的类型如String,Number,Boolean (相应的"值类型"拥有相同的方法),复杂一点的如Function,Object,Arra ...

  7. [20171031]markhot.txt

    [20171031]markhot.txt --//昨天看了https://jonathanlewis.wordpress.com/2017/10/02/markhot/,测试看看这样时候可以减少争用 ...

  8. 基于 node 搭建博客系统(一)

    系统分为两端,分别实现. 管理员端: 功能 :个人信息,设置,发布随笔,随笔列表,删除随笔,查找,文章 等. 技术点:Boostrap + AdminLTE; 基于nodejs 实现的express ...

  9. 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod

    定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...

  10. centos7安装rabbitmq 总结

    centos7下安装rabbitmq 折腾了三天最后做了以下总结 先查看一电脑名  :示例 #hostname name 查看一下hosts配置文件:如果如下结果,就要修改下 #cat /etc/ho ...