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 ...
随机推荐
- json中获取key值
<script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...
- tomcat相关配置技巧梳理 (修改站点目录、多项目部署、限制ip访问、大文件上传超时等)
tomcat常用架构:1)nginx+tomcat:即前端放一台nginx,然后通过nginx反向代理到tomcat端口(可参考:分享一例测试环境下nginx+tomcat的视频业务部署记录)2)to ...
- corosync+pacemaker的crmsh的常用指令介绍
配置crmsh的yum仓库,此仓库的RPM包有openSUSE提供,将这个network:ha-clustering:Stable.repo文件直接下载到本地并且命名为crmsh.repo wget ...
- From CORBA technology To Thrift technology
技术在变,需求不变,把复杂的事情搞简单,而不是相反. 无论CORBA还是Thrift,目标只有一个:一处定义,多处使用,解决的问题只有一个:规范和简化客户端与服务器的通信的开发工作. 是不是和java ...
- [转帖]从 2G 到 5G,手机上网话语权的三次改变
从 2G 到 5G,手机上网话语权的三次改变 美国第一大电信运营商 Verizon 公司的 CEO Hans Vestberg 手持一部 iPad,屏幕上显示俯瞰地面的飞行地图.400 多公里外的洛杉 ...
- centos7 tar.xz格式文件的解压方法
现在很多找到的软件都是tar.xz的格式的,xz 是一个使用 LZMA压缩算法的无损数据压缩文件格式. 和gzip与bzip2一样,同样支持多文件压缩,但是约定不能将多于一个的目标文件压缩进同一个档案 ...
- ARIMA模型识别、计算p、q值
#-*- coding: utf-8 -*- #确定最佳p.d.q值 import pandas as pd #参数初始化 discfile = '../data/discdata_processed ...
- scipy插值interpolation
>>> from scipy.interpolate import interp1d#interp1d表示1维插值 >>> >>> x = np. ...
- 微信小程序的界面下拉刷新
小程序的下拉刷新的值设置:需要设置app.json的window中 "navigationBarTextStyle":true
- js 实现List
js 实现List 列表是一组有序的数据.每个列表中的数据项称为元素.在 JavaScript 中,列表中的元素可以是任意数据类型. 我们可以根据数组的特性来实现List. List 抽象数据类型定义 ...