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 (<= 1010) 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
题目大意:求一个数是否是回文数,如果不是,则将与其反转数相加后,再次判断是否是回文数,以此类推。
解题:好几次没把握住数据长度,int-long-long long发现都不够,最后只能用字符型!无奈脸,从题目中推出10^10,进行100次叠加,数据可能是10^20,超过long long 的10^19;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
string add(string s,string rev){
string s1;
int length = s.size();
int i;
int index = 0;
int sum;
for(i=length-1;i>=0;i--){
sum = s[i] + rev[i] - '0' - '0' +index;
s1.insert(s1.begin(),sum%10+'0');
index = sum/10;
}
if(index){
s1.insert(s1.begin(),index+'0');
}
return s1;
}
int palindromic(string& s,string& rev){
rev = s;
reverse(rev.begin(),rev.end());
if(s == rev){
return true;
}
s = add(s,rev);
return false;
}
int main(){
string s,rev;
int k;
cin>>s>>k;
int i=0;
while(i < k){
if(palindromic(s,rev)){
break;
}
i++;
}
cout<<s<<endl<<i<<endl;
return 0;
}
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 ...
- 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 ...
- 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一开始是 ...
- 【PAT甲级】1024 Palindromic Number (25 分)
题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
随机推荐
- 《Linux内核分析》第八周
<Linux内核分析>第八周 PART ONE 知识点总结 一.进程切换的关键代码switch_to 1.进程调度与进程调度的时机 (1)进程分类: I/O型(执行块,频繁) CPU型(大 ...
- Linux内核分析——第十八章 调试
第十八章 调试 18.1 准备开始 1.在用户级的程序里,bug表现比较直接:在内核中却不清晰. 2.内核级开发的调试工作远比用户级开发艰难的多. 3.准备工作需要的是: (1)一个bug (2 ...
- java 计算器实验
1.计算器实验报告 2.https://github.com/xujinxia/text/tree/master 3.实验截图 7+8 清除 六.总结 通过本次实验让我对JFrame类.JPanel类 ...
- Alpha冲刺随笔汇总
项目Alpha冲刺(团队) Alpha冲刺随笔汇总 姓名 学号 博客链接 何守成 031602408 http://www.cnblogs.com/heshoucheng/ 黄锦峰 031602411 ...
- SQLSERVER备份恢复后权限问题简单处理.
1. 同事的服务器出现无法访问表, 应用连不上数据库... 远程了下 发现. 使用业务用户登录数据库之后查询无法下拉帮助到表, 必须增加schemas才可以访问到具体的表. 2. 问题解决. 1. 修 ...
- 《Effective C#》快速笔记(一)- C# 语言习惯
目录 一.使用属性而不是可访问的数据成员 二.使用运行时常量(readonly)而不是编译时常量(const) 三.推荐使用 is 或 as 操作符而不是强制类型转换 四.使用 Conditional ...
- Spring 入门知识点笔记整理
一.Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Sprin ...
- numpy meshgrid函数
1.meshgrid函数用两个坐标轴上的点在平面上画格. 用法: [X,Y]=meshgrid(x,y) [X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的 [X, ...
- 给kali linux2.0装一个中文输入法
没有中文输入法好痛苦啊.. 毕竟做了无限网卡,虚拟机和主机可以完完全全当两台设备使用了,所以kali还是需要一个中文输入法才方便. 由于使用的是比较新的kali版本和源,现在安装fcitx已经可以直接 ...
- redis map存储的注意点