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. split-brain 脑裂问题(Keepalived)

    脑裂(split-brain)指在一个高可用(HA)系统中,当联系着的两个节点断开联系时,本来为一个整体的系统,分裂为两个独立节点,这时两个节点开始争抢共享资源,结果会导致系统混乱,数据损坏.对于无状 ...

  2. mysql启动后随即关闭问题解决(ibdata1文件损坏导致)

    机房一台服务器上的mysql运行一段时间了,突然出现了一个很奇怪的现象:重启后无法恢复了!准确情况是:启动mysql后随即就又关闭了. 查看mysql错误日志如下: 160920 22:41:41 m ...

  3. 12.8 Daily Scrum

    最近大家都比较忙,任务今天也才刚刚分配,所以具体的编码任务从明天开始.   Tomorrow's Task 丁辛 完善餐厅列表,显示距离.             邓亚梅          美化搜索框 ...

  4. 《Linux内核设计与实现》第十八章学习笔记

    第十八章 调试 [学习时间:1小时 总结博客时间:1小时15分] [学习内容:出现bug的原因.内核调试器gdb.使用Git进行二分查找] 内核级开发的调试工作远比用户级开发艰难,它带来的风险比用户级 ...

  5. 冲刺Two之站立会议9

    今天我们团队主要针对软件的功能进行了改进.因为它目前可以实现视频通话,语音聊天,文件传输和文字聊天的通信功能,我们想要在它的基础上实现临时局域群聊和群聊视频的功能,目前还没有完全实现.

  6. <<梦断代码>>阅读笔记二

    这是第二篇读书笔记,这本书我已经读了有一大半了,感觉书中所描述的人都是疯子,一群有创造力,却又耐得住寂寞的疯子. 我从书中发现几点我比较感兴趣的内容. 第一个,乐高之梦.将程序用乐高积木一样拼接起来. ...

  7. 用软件工程分析开源项目octave的移植

    在本科的时候学习了软件工程,报考了信息系统项目管理师的考试,,虽然没有过,但是其实还是学了一些相关项目管理方面的知识,,9大管理,,当年应该能背出来,,, 1 项目整体管理 2 项目范围管理 3 项目 ...

  8. 小学生四则运算APP核心代码公布

    Mainactivity类: package com.example.XXSCYS; import java.io.ByteArrayOutputStream; import java.io.File ...

  9. 第三个spring冲刺第3天

    基本功能跟界面都完成了,今天小组开了个会,基于跟别的小组对比的效果,感觉自己组的效果没别人的好,很多方面还欠缺,所以我们会继续跟进完善.

  10. (转)SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间

    原因: 出现这种问题多半是因为你插入或者更新数据库时,datetime字段值为空默认插入0001年01月01日造成datetime类型溢出. 传给数据库表的时间类型值是null值.这里的null指的是 ...