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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
char str[];
typedef struct info{
int num[];
int len;
info(){
for(int i = ; i < ; i++)
num[i] = ;
len = ;
}
}bign;
void reverse(bign &a, bign &b){
b.len = ;
for(int i = a.len - ; i >= ; i--){
b.num[b.len++] = a.num[i];
}
}
bign add(bign a, bign b){
bign c;
int carry = ;
for(int i = ; i < a.len || i < b.len; i++){
int temp = carry + a.num[i] + b.num[i];
c.num[c.len++] = temp % ;
carry = temp / ;
}
if(carry != ){
c.num[c.len++] = carry;
}
return c;
}
int isPal(bign a){
for(int i = , j = a.len - ; i <= j; i++, j--){
if(a.num[i] != a.num[j])
return ;
}
return ;
}
bign a, b, c;
int main(){
int K;
scanf("%s %d", str, &K);
for(int i = strlen(str) - ; i >= ; i--){
a.num[a.len++] = str[i] - '';
}
int find = ;
if(isPal(a)){
find = ;
for(int j = a.len - ; j >= ; j--){
printf("%d", a.num[j]);
}
printf("\n%d", );
}
for(int i = ; find == && i < K; i++){
reverse(a, b);
a = add(a, b);
if(isPal(a)){
find = ;
for(int j = a.len - ; j >= ; j--){
printf("%d", a.num[j]);
}
printf("\n%d", i + );
break;
}
}
if(find == ){
for(int j = a.len - ; j >= ; j--){
printf("%d", a.num[j]);
}
printf("\n%d", K);
}
cin >> K;
return ;
}

总结:

1、仍然是大整数加法,注意有可能给出的数不用做处理就是对称数,这是应输出这个数,次数位0。

A1024. Palindromic Number的更多相关文章

  1. PAT A1024 Palindromic Number (25 分)——回文,大整数

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

  2. PAT甲级——A1024 Palindromic Number

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

  3. PAT_A1024#Palindromic Number

    Source: PAT A1024 Palindromic Number (25 分) Description: A number that will be the same when it is w ...

  4. General Palindromic Number (进制)

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

  5. Palindromic Number (还是大数)

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

  6. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  7. PAT1019:General Palindromic Number

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

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

  9. PAT A1019 General Palindromic Number (20 分)——回文,进制转换

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

随机推荐

  1. asp.net mvc接收安卓post的json字符串

    筛选器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syst ...

  2. Unity接入Steamworks

    一.将scrpts/Steamworks.net/SteamManager组件添加到游戏物体上 二.修改SteamManager的代码为游戏的id如图所示 三.Unity,打开项目根目录,修改stea ...

  3. SpringBoot笔记--Jackson

    SpringUtil.getBean<GenericConversionService>().addConverter(Date2LocalDateTimeConverter()) var ...

  4. libc.so.6: version 'GLIBC_2.14' not found报错提示的解决方案

    线上一台服务器在执行leveldb程序的时候,报错:"libc.so.6: version `GLIBC_2.14' not found". 排查原因及解决方法如下: 1)产生原因 ...

  5. Docker容器学习梳理 - Dockerfile构建镜像

    在Docker的运用中,从下载镜像,启动容器,在容器中输入命令来运行程序,这些命令都是手工一条条往里输入的,无法重复利用,而且效率很低.所以就需要一 种文件或脚本,我们把想执行的操作以命令的方式写入其 ...

  6. 【读书笔记】Linux内核设计与实现(第三章)

    3.1 进程 处于执行期的程序. 进程就是正在执行的程序代码的实时结果.内核需要有效而又透明地管理所有细节. 执行线程(简称线程):在进程中活动的对象.每个线程都拥有一个独立的程序计数器.进程栈和一组 ...

  7. 《Linux内核分析》第八周学习笔记

    <Linux内核分析>第八周学习笔记 进程的切换和系统的一般执行过程 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163 ...

  8. Android Studio中的Gradle是干什么的

    作者:ghui链接:https://www.zhihu.com/question/30432152/answer/48239946来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  9. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  10. maven 总结