PAT Advanced 1024 Palindromic Number (25) [数学问题-⼤整数相加]
题目
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成为回文数
- N若为最大值1010操作100次大于1020,超过long long类型范围,需要看做大整数处理数据(string)
解题思路
- 判断N是否为回文数,如果不是回文数,进入循环操作,直到操作次数达到k或者N成为回文数
- 大整数处理,需要逆置字符串,因为操作从数字的个位开始(但是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) [数学问题-⼤整数相加]的更多相关文章
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- 【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 ...
- 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 ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 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 ...
- 1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- 1024 Palindromic Number (25 分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) 1024. Palindromic Number (25)
手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...
- PAT甲题题解-1024. Palindromic Number (25)-大数运算
大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...
随机推荐
- 154-PHP strpos函数
<?php $str='passwords'; //定义一个字符串 $position=strpos($str,'s'); //查找字母s第一次出现的位置 echo '字母s的位置是'.$pos ...
- php知识结构
PHP的运行环境 连环境都搞不起来,就是你有多么喜欢PHP,那也是白搭,开始我们大多会使用集成环境软件例如xampp,wamp.随着知识的增加慢慢要学会自己搭建运行环境,例如 Linux(Ubuntu ...
- 如何让手游更省带宽,耗电量更少?TBR渲染架构解析!
如何让手游更省带宽,耗电量更少?渲染或是其中一个可突破的点.本文中,腾讯游戏学院专家Hailong将从为大家解析TBR渲染架构的特点. 什么是TBR? 全称是Tile Based Rendering, ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 基本的输入输出
C++ 的 I/O 发生在流中,流是字节序列.如果字节流是从设备(如键盘.磁盘驱动器.网络连接等)流向内存,这叫做输入操作.如果字节流是从内存流向设备(如显示屏.打印机.磁盘驱动器.网络连接等),这叫 ...
- 解决Spring Mvc中接受参数绑定重名的方法
html页面 <form method='post' action='url'> 用户名 <input type='text' name='name'> 用户id <in ...
- check Linux文件夹个数
(ls -l|grep "^-"| wc -l)查看某个文件夹下文件的个数.(ls -lR|grep "^-"| wc -l)查看某个文件夹下文件的个数,包括子 ...
- mini2440 裸机程序,下载到nand 里面,复制到 sdram 中不运行
按照韦东山的视频中 sdram的裸机代码,写了一份,通过 minitools 下载到 0x30000000,然后烧录到 nand中,接过不能正常运行. 尝试过多种方法后,只有一种解决方法,就是不要用 ...
- VLOOKUP返回#N/A结果
VLOOKUP返回#N/A结果 1.无目标值 使用control+f查找是否存在所要搜索的值. 2.位置错误 所要搜索区域,被搜索值必须在首列. 3.格式错误 搜索值和被搜索区域格式需一致. 4.特殊 ...
- 代码神器:拒绝重复编码,这款IDEA插件了解一下.....
作者:HeloWxl www.jianshu.com/p/e4192d7c6844 Easycode是idea的一个插件,可以直接对数据的表生成entity.controller.service.da ...
- POJ 1840:Eqs 哈希求解五元方程
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14169 Accepted: 6972 Description ...