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. EBS查找当前Form文件

    http://www.cnblogs.com/benio/archive/2011/06/10/2077289.html 我们经常会要在ORACLE EBS中寻找我们正在浏览的form页面的执行文件, ...

  2. 【转】如何使用BehaviorSDK

    原文地址:http://blogs.msdn.com/b/windows8devsupport/archive/2014/10/24/behaviorsdk.aspx 前言 在开发过程中,程序员一般通 ...

  3. hsweb 企业后台管理基础框架

    hsweb 详细介绍 业务功能 现在: 权限管理: 权限资源-角色-用户. 配置管理: kv结构,自定义配置.可通过此功能配置数据字典. 脚本管理: 动态脚本,支持javascript,groovy, ...

  4. NetCore入门篇:(六)Net Core项目使用Controller之一

    一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择完全抛弃传统mvc模式,使用html + webapi模 ...

  5. WPF点滴(1) Main 函数

    应用程序的入口函数是main函数,在Console程序和Winform程序main函数都有清晰的定义,可以很容易找到,但是WPF的工程文件中却找不到main函数的定义,是WPF不需要main函数吗?N ...

  6. day52 进程与守护进程

    http://www.cnblogs.com/Eva-J/articles/8253549.html 博客参考. 多进程聊天 守护进程. 多进程 1.Unix/Linux:fork()调用实现多进程. ...

  7. javaWeb登录注册页面

    简单的登陆注册页面 1.配置JDBC驱动连接数据库 2. 配置struts2框架 3. 利用1 2完成登录页面, 注意做到不耦合,即servlet Api和控制器完全脱离) 4. 利用1 2 制作注册 ...

  8. leetcode-914-卡牌分组

    题目描述: 给定一副牌,每张牌上都写着一个整数. 此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组: 每组都有 X 张牌. 组内所有的牌上都写着相同的整数. 仅当你可选的 ...

  9. 判断easyUI tree 节点复选框是否被选中的方法。将选中的节点高亮显示的方法

    在datagrid tree中如何判断某个节点的复选框是否被选中? 我们可以通过HTML结构去分析: 1.节点未选中前 2.节点选中后 所以节点被选中前和选中后,html中的class类是用区分的. ...

  10. 使用图片预加载,解决断网后无法从后台获取提示网络异常的logo图片的问题

    项目中有需求,断网后,显示小提示窗,里面包含网络异常提示语和异常小logo图片. 在实际操作时,遇到,断网后,无法从后台获取异常小logo图片. 我是才用图片预加载的方法解决这个问题的,解决方法如下: ...