题目

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 nonpalindromic 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 afer 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

题目分析

给出一个整数N(<=10^10),最大操作次数K(<=100),若整数不为回文数,进行如下操作(1. 反转N得到FN;2.N=FN+N),循环执行,直到操作次数达到K次或者N成为回文数

  1. N若为最大值1010操作100次大于1020,超过long long类型范围,需要看做大整数处理数据(string)

解题思路

  1. 判断N是否为回文数,如果不是回文数,进入循环操作,直到操作次数达到k或者N成为回文数
  2. 大整数处理,需要逆置字符串,因为操作从数字的个位开始(但是a,b互为反转,所以本题中只需要反转一个就可以)

Code

Code 01

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
bool isPac(string & s) {
for(int i=0; i<s.length(); i++) {
if(i>=s.length()-1-i)break;
if(s[i]!=s[s.length()-1-i])return false;
}
return true;
}
string bignadd(string a) {
// b是a的反转 长度一定相等,无需考虑不相等的情况
string b = a;
reverse(b.begin(),b.end());
int carry=0;
string c;
for(int i=0; i<a.length(); i++) {
int tempa = a[i]-'0',tempb = b[i]-'0';
int temp = tempa+tempb+carry;
c.push_back(temp%10+'0');
carry=temp/10;
}
if(carry==1)c.push_back('1');
reverse(c.begin(),c.end());
return c;
}
int main(int argc,char *argv[]) {
string n,temp;
int m;
cin>>n>>m;
temp = n;
int i=1;
for(i=1; i<=m&&!isPac(temp); i++) {
temp = bignadd(temp);
}
printf("%s\n",temp.c_str());
printf("%d",i-1);
return 0;
}

PAT Advanced 1024 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)

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

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

  4. PAT 甲级 1024 Palindromic Number

    1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  5. 1024 Palindromic Number (25)(25 point(s))

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

  6. 1024. Palindromic Number (25)

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

  7. 1024 Palindromic Number (25 分)

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

  8. PAT (Advanced Level) 1024. Palindromic Number (25)

    手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...

  9. PAT甲题题解-1024. Palindromic Number (25)-大数运算

    大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...

随机推荐

  1. 六十一、SAP中的逻辑运算与进制转换

    一.代码如下 二.16进制计算过程如下 三.计算结果为16进制的11,也就是10进制的17

  2. 002、将mysql用作一个简单的计算器

    SELECT PI( ), , ( ) ; 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢.

  3. ES6 之 Reflect 的方法总结

    1. 概述 将 Object 对象的一些明显属于语言内部的方法(比如 Object.defineProperty ),放到 Reflect 对象上. 修改某些 Object 方法的返回结果,让其变得更 ...

  4. Python MongoDB 插入文档

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. Node.js NPM 作用

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json NPM ...

  6. Django static配置

    STATIC_URL = '/static/' # HTML中使用的静态文件夹前缀 STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static&q ...

  7. Mysql: if 结构

    if结构 语法 if  条件1  then  语句1; elseif   条件2   then  语句2; ... else 语句n;   # 可以不写 应用场合:应用在begin end 中 SEL ...

  8. java反射的学习

    1.类的 类类型(ClassType) 类的类类型可以用来做很多事,我们可以通过它获取到类的名称,类的路径,类的成员变量,类的方法等等,还可以通过它获得类的实例化对象. 我们可以通过 类名.class ...

  9. AD在更新PCB的时候,每次封装都会改变位置?

    转载:https://blog.csdn.net/abc87891842/article/details/52538660 3.如果是很多元件的ID不一致, 手动修改太麻烦了, 可以使用AD的 &qu ...

  10. Atom 插件推荐

    (1)atom-ternjs : js(e6)的自动补充 (2)key-binding-mode : atom 快捷键管理 (3)pre-view : pdf预览 (4)activate-power- ...