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)的更多相关文章

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

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

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

  5. 1024 Palindromic Number (25 分)

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

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

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

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

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

  8. 【PAT甲级】1024 Palindromic Number (25 分)

    题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...

  9. PAT 甲级 1024 Palindromic Number

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

随机推荐

  1. LINUX第三次实践:程序破解

    LINUX第三次实践:程序破解 标签(空格分隔): 20135328陈都 一.掌握NOP.JNE.JE.JMP.CMP汇编指令的机器码 NOP:NOP指令即"空指令".执行到NOP ...

  2. 常见IP端口

    21端口:21端口主要用于FTP(File Transfer Protocol,文件传输协议)服务. 23端口:23端口主要用于Telnet(远程登录)服务,是Internet上普遍采用的登录和仿真程 ...

  3. CRM 数据查重

    2.8 小工具 · 纷享销客产品手册https://www.fxiaoke.com/mob/guide/crmdoc/src/2-8%E5%B0%8F%E5%B7%A5%E5%85%B7.html C ...

  4. Delphi/XE2 使用TIdHttp控件下载Https协议服务器文件[转]

    之前的一篇博文详细描述了使用TIdhttp控件下载http协议的文件,在我项目的使用过程中发现对于下载Https协议中的文件与Http协议的文件不同,毕竟Https在HTTP协议基础上增加了SSL协议 ...

  5. [转帖] “王者对战”之 MySQL 8 vs PostgreSQL 10

    原贴地址:https://www.oschina.net/translate/showdown-mysql-8-vs-postgresql-10?lang=chs&page=2# 英文原版地址 ...

  6. From 简书 转帖一下如何安装k8s1.10 改天做下实验. https://www.jianshu.com/p/9c7e1c957752

    centos7.3 kubernetes/k8s 1.10 离线安装 老菜_misa 关注 2018.04.25 23:57 字数 1243 阅读 266评论 1喜欢 3 本文介绍在centos7.3 ...

  7. thread run 和 start 的区别

    run 方法 也可以调用线程启动   但是单线程(为顺序执行) 而start方法 启动的线程为多个线程之间争夺cpu的执行权(为随机的) 摘录于http://www.cnblogs.com/sunfl ...

  8. 使用highlightjs自定义markdown代码高亮

    目录 概述 实现方法 概述 最近使用markdown来写一些技术文档和博客,觉得真心不错,这才是程序员该用的编辑器嘛~~ Mou在mac上的 markdown 编辑器,很简约,可惜Mou好像只支持标准 ...

  9. 配置自己的Maven方式并使用Maven 运行项目Idea的maven的项目

    (1) 当安装了 maven之后,需要导入项目代码,然后编译执行: 打开Idea ==>然后点击小扳手==>在搜索框中输入maven==>然后找到 Maven home direct ...

  10. 设计模式笔记:适配器模式(Adapter)

    1. 适配器模式简介 1.1 模式定义 适配器模式:通过一个类的接口转换成客户希望的另外一个接口,使原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适配器从结构上分为:类适配器和对象适配器.其 ...