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 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
题目分析:大数加法 把是否进1记录就好了
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
using namespace std;
string Add(string s1, string s2)
{
int flag = ;
int len = s1.length();
for (int i = len - ; i >= ; i--){
int temp = flag+s1[i] - '' + s2[i] - '';
if (temp > ){
s1[i] = (temp % ) + '';
flag = ;
}
else {
s1[i] = temp + '';
flag = ;
}
}
if (flag)return '' + s1;
else
return s1;
}
int main()
{
string N;
int K;
cin >> N >> K;
int i = ;
for (; i < K; i++){
string s =N;
reverse(s.begin(), s.end());
if (s == N)
break;
else
N = Add(N, s);
}
cout << N << endl << i;
}
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 分)
题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...
- 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)(25 point(s))
problem A number that will be the same when it is written forwards or backwards is known as a Palind ...
- 【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 ...
- 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 ...
- 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一开始是 ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
随机推荐
- JS数组遍历方法集合
就让我们在逆战中成长吧,加油武汉,加油自己 1.for循环 使用零时变量将长度存起来,当数组较大时优化效果才会比较明显. var ar1=[2,4,6,8] for(var i=0;i<ar1. ...
- 树莓派3B+远程VNC的设置
现在很少有自带VNCserver的教程,因为之前官方系统没有自带VNC,但是现在最新版的官方系统已经自带VNCserver,只需要在设置里启用一下,然后设置就可以用啦. 1.打开树莓派设置 sudo ...
- 关于BitmapImage EndInit()时报值不在范围内的异常
值不在预期的范围内.ArgumentException 在 System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorConte ...
- JDBC封装-Java(新手)
JDBC的封装,自己总结的自己总结的自己总结的 dao (代码分层)命名规范: 1.com.XXX.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 2. ...
- 2019.3.14解题报告&补题报告
A题 题意: 输入r, c,代表r*c的矩阵,接下来一行,是r个数,代表每一行里最大的数:接下来一行,是c个数,代表每一列中的最大数.求所给数据是否冲突. 思路:判断r个数中最大数maxr和c个数中最 ...
- iOS/macOS推荐个高效苹果开发工具, JSON 转模型代码工具,不再为复杂JSON数据写模型而烦恼,支持Swift/Objective-C,极速转换
CCJSON 是一款运行在macOS上 JSON 转模型代码工具,不再为复杂JSON数据写模型而烦恼,可识别嵌套模型,字典/数组,支持Swift/Objective-C,操作方便,极速转换.下载 效果 ...
- 【转】使用ssh-copy-id 快速的配置免密登录
1.在需要免密登录其他机器的主机上 生成公钥,私钥等. ssh-keygen -t rsa 回车回车回车 哪个用户登录就在哪个用户目录的.ssh目录下生成. 2.将以下命令做成脚本,因为环境不能下载, ...
- git 使用和一些错误
一.简单使用 Git是目前世界上最先进的分布式版本控制系统,用于自动记录每次文件的改动,但是和所有版本控制系统一样,只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等,而图片.视频这些二进 ...
- kerberos系列之hive认证配置
大数据安全系列之hive的kerberos认证配置,其它系列链接如下 https://www.cnblogs.com/bainianminguo/p/12548076.html-----------安 ...
- 常用正则表达式(手机号、邮箱、URL地址、身份证等等)
一.前言 不好的习惯:1.每一次用到正则都是上网copy一份,也没有去学习思考,看看都是什么意思: 2.一个项目里不同的地方用到了相同的校验,一直在重复的copy代码,并没有统一起来,万一哪天要修改规 ...