A1024. 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 (<= 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的更多相关文章
- 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 ...
- PAT甲级——A1024 Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT_A1024#Palindromic Number
Source: PAT A1024 Palindromic Number (25 分) Description: A number that will be the same when it is w ...
- General Palindromic Number (进制)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- Palindromic Number (还是大数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limit: 65536 KB A number that will ...
- PAT1019:General Palindromic Number
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 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 ...
- 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 ...
随机推荐
- (9)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- JWT算法
一. JWT 简介 内部 Restful 接口可以“我家大门常打开”,但是如果要给 app 等使用的接口,则需要做权限校验,不能谁都随便调用. Restful 接口不是 web 网站,App 中很难直 ...
- Oracle数据库冷备份与热备份操作梳理
Oracle数据库的备份方式有冷备份和热备份两种,针对这两种备份的实施过程记录如下: 一.Oracle冷备份 概念数据库在关闭状态下完成所有物理系统文件拷贝的过程,也称脱机备份.适合于非归档模式(即n ...
- Mysql读写分离方案-Amoeba环境部署记录
Mysql的读写分离可以使用MySQL Proxy,也可以使用Amoeba.Amoeba(变形虫)项目是一个类似MySQL Proxy的分布式数据库中间代理层软件,是由陈思儒开发的一个开源的java项 ...
- jsonrpc环境搭建和简单实例
一.环境准备 下载需要的jar包和js文件,下载地址:https://yunpan.cn/cxvbm9DhK9tDq 访问密码 6a50 二.新建一个web工程,jsonrpc-1.0.jar复制到 ...
- 10 分钟理解 BFC 原理
一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: 普通流 (normal flow) 在普通流中,元素按照其在 HTML 中的先后位置至 ...
- og标签对SEO的作用及用法
meta property=og标签对SEO的作用及用法,如果你仔细观察会发现本站点<head>代码中有一段:"property="og:image"这段代码 ...
- mysql 编码和汉字存储占用字节问题的探索
MySql 5.5 之前,UTF8 编码只支持1-3个字节,只支持BMP这部分的unicode编码区,BMP是从哪到哪?基本就是 0000 ~ FFFF 这一区. 从MySQL 5.5 开始,可支持4 ...
- 个人博客作业-Week2 (代码规范, 代码复审)
代码规范: 1.这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 编码规范它包含了代码格式,还包括了编码风格和其他规范,通常涉及:缩进.空格使用.Tab使用 注释. ...
- Github上传更新
通过2天的时间,不停的网上找各种资料,今天下午终于可以登录上github for Windows 客户端了,,, 然后通过一整晚的摸索,也把项目上传到github里. github地址:https:/ ...
- IP工具类
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...