PTA (Advanced Level) 1024 Palindromic Number
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的更多相关文章
- PAT (Advanced Level) 1024. Palindromic Number (25)
手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...
- 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 ...
- PAT 甲级 1024 Palindromic Number
1024. Palindromic Number (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- 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 ...
- 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 ...
- 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 ...
- 1024. Palindromic Number (25)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT 1024 Palindromic Number[难]
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
随机推荐
- (原创)hibernate 一对多建表实例详解 附上各个注释的含义
这个是hibernate的一对多建表实例:一的一端是部门(Department),对的一端是员工(Employee),下面贴上成员源代码:其中@mappedBy是加在@OneToMany一端,并且它的 ...
- Tomcat监听443端口的方法
当我们需要更安全的访问网站的时候就会选择使用https协议,而https协议默认的端口号为443端口,这就是我们为什么向让Tomcat监听在443端口的原因,因为监控在非80端口和443端口的web服 ...
- Ubuntu-18.04.2系统 Nginx+uWSGI+Django 部署生产环境
首先准备环境: 1.使用虚拟机 VMware Workstation Pro (也可以不用),安装Ubuntu-18.04.2系统 开始搭建环境 (因为ubuntu18.4.2集成了python3.6 ...
- NetCore入门篇:(五)Net Core项目使用静态文件
一.简介 1.Net Core默认无法访问静态文件,需要在Startup通过代码添加定义. 2.本文介绍两种静态文件目录实现方式. 二.启用默认目录 1.添加图片文件 2.测试访问结果(不能访问) 3 ...
- SSM文件上传
**自己对于SSM文件上传的一些心得** 刚开始的时候也是在网上寻找一些简单的案例,可能我的这篇文章不是最好的,但是这些都是我自己慢慢的摸索以及自己的尝试的一些心得,希望对各位有所帮助. 其实文件的上 ...
- 【ZOJ2314】Reactor Cooling(有上下界的网络流)
前言 话说有上下界的网络流好像全机房就我一个人会手动滑稽,当然这是不可能的 Solution 其实这道题目就是一道板子题,主要讲解一下怎么做无源无汇的上下界最大流: 算法步骤 1.将每条边转换成0~u ...
- Heap-451. Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- MySQL(ORM框架)
day63 参考:http://www.cnblogs.com/wupeiqi/articles/5713330.html SQLAlchemy本身无法操作数据库,其必须以来pymsql等第三方插件, ...
- Mybatis的cache
相关类:org.apache.ibatis.executor.CachingExecutor 相关代码: public <E> List<E> query(MappedStat ...
- 使用hex编码绕过主机卫士IIS版本继续注入
本文作者:非主流 测试文件的源码如下: 我们先直接加上单引号试试: http://192.168.0.20/conn.asp?id=1%27 很好,没有报错.那我们继续,and 1=1 和and 1= ...