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. c#基础系列3---深入理解ref 和out

    "大菜":源于自己刚踏入猿途混沌时起,自我感觉不是一般的菜,因而得名"大菜",于自身共勉. 扩展阅读 c#基础系列1---深入理解 值类型和引用类型 c#基础系 ...

  2. Salesforce随笔: 将Visualforce Page导出为 Excel/CSV/txt (Display a page in Excel)

    想要实现如题所述功能,可以参照 : Visualforce Developer Guide 第57页中所举的例子,在<apex:page>标签中添加contentType属性. <a ...

  3. ResultSet集合查询字段名称(转载)

    转自:https://blog.csdn.net/song_litao/article/details/84751351 public List<String> getColumnName ...

  4. PairProject——结对编程

    成员:12061162  王骜 12061225  钟毅恒 一.合作过程中的照片 . 二.结对编程的优缺点 优点: 1)在编程过程中,任何一段代码都不断地复审,同时避免了将写代码的责任抛给一个人的问题 ...

  5. 个人项目Week1

    一.项目时间规划与实际用时 PSP2.1 Personal Software Process Stages 预计时间/h 实际时间/h Planning 计划     · Estimate · 估计这 ...

  6. Linux内核分析(第八周)

    进程的切换和系统的一般执行过程 一.进程切换的关键代码switch_to分析 1.进程调度与其时机分析 分类: 第一种分类 I/O-bound:频繁的进行I/O:会花很多时间等待I/O操作完成 CPU ...

  7. 关于本科毕业论文《Laguerre小波在数值积分与微分方程数值解中的应用》存在的问题与小结

    本科的毕业设计<Laguerre小波在数值积分与微分方程数值解中的应用>是通过Laguerre小波函数来近似表达某个需要求积分或解微分方程的函数,将原函数很难求得函数用小波函数表达出来,这 ...

  8. mybatis 框架网站

    http://www.mybatis.org/mybatis-3/zh/index.html

  9. Mind Manager X 10 registry backup key under windows XP

    Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Mindjet\MindManager\10] [HKEY_CURRE ...

  10. React 支持JS

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...