题目

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. 092-PHP定义索引数组

    <?php $arr=array('name'=>'PHP','age'=>22,7=>25,33,21=>35,56); //定义一个索引数组 echo '数组$arr ...

  2. int, float, double 等转化为 string

    一般有以下两种方法: QVecotr<int> vec; QString(QByteArray().setNum(vec.at(3))) float f; QString("%1 ...

  3. js 加密解密 TripleDES

    <!DOCTYPE html> <html lang="en">   <head>     <meta charset="UTF ...

  4. Python抽象基类之声明协议

    抽象基类之--声明协议 上回讲了Python中抽象基类的大概,相信大家对abcmeta以及什么是抽象基类已经有所了解.传送门 现在我们来讲讲抽象基类的另一个常用用法--声明协议 所谓声明协议,有点像J ...

  5. 【LeetCode】509. 斐波那契数

    题目 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0,   F(1) = 1 F(N) = ...

  6. 学习EIGRP 笔记

    CEFFIB(转发信息库,RIB现在运行了CEF,就称之为FIB)show ip cef detail EIGRP的基本组件:1.邻居发现机制2.可靠传输协议(RTP机制)3.DUAL算法4.多种网络 ...

  7. zabbix_agent_win

    http://mayulin.blog.51cto.com/1628315/514447/ http://www.cnblogs.com/likehua/p/3968689.html 先下载win客户 ...

  8. Android Studio Madual作为application的使用以及工作空间和modual的区别

    Android Studio Madual作为application的使用以及工作空间和modual的区别 前言: 写这篇文章的目的是因为自己使用Android Studio开发时进入了一个误区,后面 ...

  9. 2.6 UI控件与后台联系实现

    完成的结果如下 : 当点击 左按钮时 最上边的显示栏更改为左 反之则为右  点击开关显示为开或者关 下边两个为显示加载的界面 在输入栏输入数值可以控制进度条的百分比并且显示在最上边 点击图片一二切换图 ...

  10. 关于Pytorch中autograd和backward的一些笔记

    参考自<Pytorch autograd,backward详解>: 1 Tensor Pytorch中所有的计算其实都可以回归到Tensor上,所以有必要重新认识一下Tensor. 如果我 ...