Palindromic Number

  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 (≤) is the initial numer and K (≤) 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 Kinstead.

Sample Input 1:

67 3

Sample Output 1:

484
2

Sample Input 2:

69 3

Sample Output 2:

1353
3

题目解析
  本题给出两个数字n与k,n代表未转换数字,k代表最大转换步数,如果n不是回文数字屎就执行将n反转,n + 反转后的数字,若n还不是回文数字则继续执行上述操作,直到n为回文数字或执行次数超过最大转换步数为止。

  本题数据可能超过int范围,既然既要判断回文又要反转数字,那么索性之间用处理大数的方法——用字符串记录数字。

  之后模拟出加法操作与反转操作,判断执行后的数组是否为回文串即可。

AC代码

 #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3;
struct Number{ //记录数字
int num[MAX]; //存储数字的每一位(为了方便加法运算倒序存储)
int len; //存储数字位数
Number(){ //构造函数
memset(num, , sizeof(num)); //初始化num每一位都为0
len = ; //位数为0
}
};
Number toNumber(string temp){ //字符串转化为Number
Number a;
a.len = temp.size();
for(int i = ; i < a.len; i++) //倒序存储
a.num[i] = temp[a.len - i - ] - '';
return a;
}
Number add(Number a, Number b){ //加法函数
Number c;
int carry = ; //进位
for(int i = ; i < a.len || i < b.len; i++){ //以较长的长度为界限(当然本题两个数长度相等,其实以任意一个为界都可以)
int temp = a.num[i] + b.num[i] + carry; //两个位置相加后加上进位
c.num[c.len++] = temp % ; //记录新数字的对应位
carry = temp / ; //计算新的进位
}
if(carry != ) //若最高位有进位
c.num[c.len++] = carry;
return c;
}
Number reversed(Number a){ //反转函数
Number b;
for(int i = a.len - ; i >= ; i--){
b.num[b.len++] = a.num[i];
}
return b; }
bool judgePalindromic(Number a){ //判断回文
for(int i = ; i < a.len / ; i++){
if(a.num[i] != a.num[a.len - i - ]) //从两端像中间匹配,若失匹返回false
return false;
}
return true;
}
int main()
{
string str;
int k;
cin >> str >> k; //输入数字与最大转化次数
Number n = toNumber(str); //将数字转化为Number型
int cnt = ; //cnt记录当前转化次数
while(cnt < k && !judgePalindromic(n)){ //转化次数超限或成功转化为回文时结束循环
Number temp = reversed(n); //反转
n = add(n, temp); //相加
cnt++; //转化次数加1
}
for(int i = n.len - ; i >= ; i--) //输出转化后的数字
printf("%d", n.num[i]);
printf("\n");
printf("%d\n", cnt); //输出转化次数
return ;
}

PTA (Advanced Level) 1024 Palindromic Number的更多相关文章

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

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

  2. PTA (Advanced Level)1082.Read Number in Chinese

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...

  3. PAT 甲级 1024 Palindromic Number

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

  4. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  5. PTA (Advanced Level) 1019 General Palindromic Number

    General Palindromic Number A number that will be the same when it is written forwards or backwards i ...

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

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

  8. 1024. Palindromic Number (25)

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

  9. PAT 1024 Palindromic Number[难]

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

随机推荐

  1. Android 体系架构

    什么是Android? 答:Android就是移动设备的软件栈,包括(一个完整的操作系统,中间件,关键应用程序), 底层是Linux内核,包括(安全管理, 内存管理,进程管理 ,电源管理,硬件驱动-) ...

  2. DBCC--OPENTRAN

    返回最早开始的但仍在运行的事务 数据库 'DB1' 的事务信息. 最早的活动事务: SPID (服务器进程 ID): 60 UID (用户 ID): -1 名称          : user_tra ...

  3. c#中取整方式

    主要用到 System 命名空间下的一个数据类 Math ,调用他的方法 一共有三种方式: 第一种 Math.Round:根据四舍五入取整 第二种 Math.Ceiling:向上取整,有小数,整数加1 ...

  4. [.net 多线程]线程基础

    一.线程状态 1.新建状态(New):         当用new操作符创建一个线程时, 例如new Thread(r),线程还没有开始运行,此时线程处在新建状态. 当一个线程处于新生状态时,程序还没 ...

  5. Docker容器的原理与实践 (下)

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Docker原理分析 Docker架构 镜像原理 镜像是一个只读的容器模板,含有启动docker容器所需的文件系统结构及内容Docker以镜像和在镜 ...

  6. Python 面向对象编程的一些特征 及 单例模式的实现, 父类方法的调用(python2/python3)

    一.面向对象编程几个特征(封装, 继承,多态) 1.封装:类里面封装函数和变量, 在将类进行实例化成实例对象时进行传参, 从而生成不同的实例对象,增加代码的复用. 2.继承:子类可以继承父类的方法和属 ...

  7. Django(wsgiref、jinja2模块使用介绍)

    day60 wsgiref比较稳定 """ 根据URL中不同的路径返回不同的内容--函数进阶版 返回HTML页面 让网页动态起来 wsgiref模块版 "&qu ...

  8. hdoj2604 Queuing(矩阵快速幂)

    此题如果直接利用递推关系,处理不好会超内存的. 首先找出递推关系式,先给出递推关系式:( L )=( L - 1 ) + ( L - 3 ) + ( L - 4 ):可以先尝试推导一下,推不出来再看下 ...

  9. php程序开销比较

    内存最快 文件次之 数据库最慢

  10. 《JAVA与模式》之装饰模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述装饰(Decorator)模式的: 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替 ...