pat 1136 A Delayed Palindrome(20 分)
1136 A Delayed Palindrome(20 分)
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 with 0≤ai<10 for all i and ak>0. Then N is palindromic if and only if ai=ak−i for all i. Zero is written 0 and is also palindromic by definition.
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. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )
Given any positive integer, you are supposed to find its paired palindromic number.
Input Specification:
Each input file contains one test case which gives a positive integer no more than 1000 digits.
Output Specification:
For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:
A + B = C
where A
is the original number, B
is the reversed A
, and C
is their sum. A
starts being the input number, and this process ends until C
becomes a palindromic number -- in this case we print in the last line C is a palindromic number.
; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations.
instead.
Sample Input 1:
97152
Sample Output 1:
97152 + 25179 = 122331
122331 + 133221 = 255552
255552 is a palindromic number.
Sample Input 2:
196
Sample Output 2:
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
52514 + 41525 = 94039
94039 + 93049 = 187088
187088 + 880781 = 1067869
1067869 + 9687601 = 10755470
10755470 + 07455701 = 18211171
Not found in 10 iterations.
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define wzf ((1 + sqrt(5.0)) / 2.0)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 2e3 + ; char A[MAXN], B[MAXN], C[MAXN]; void calcB()
{
int len = strlen(A), a = len - , b = ;
for (a ,b ; b < len; ++ b, -- a)
B[b] = A[a];
} void calcC()
{
int len1 = strlen(A), len = len1, b = ;
int temp[MAXN];
for (int i = , j = len1 - ; i < len; ++ i, -- j)
{
if (j != -) b += int(A[j] - '') + int(B[j] - '');
temp[i] = b % ;
b /= ;
if (i == len - && b > ) ++ len;
} for (int i = , j = len - ; i < len; ++ i, -- j)
C[i] = char ('' + temp[j]);
} int main()
{
scanf("%s", &A);
int len = strlen(A), a = len - , b = ;
for (a ,b ; b < len; ++ b, -- a)
B[b] = A[a];
for (int i = ; ; ++ i)
{
if (i == )
{
printf("Not found in 10 iterations.\n");
break;
}
calcB();
if (strcmp(A, B) == )
{
printf("%s is a palindromic number.\n", A);
break;
}
calcC();
printf("%s + %s = %s\n", A, B, C);
strcpy(A, C);
}
return ;
}
最新文章
- JavaScript 中的变量命名方法
- Highchart基础教程-图表配置
- .Net Core Linux centos7行—IOC模块
- oracle入门必备
- yaourt: a pacman frontend(pacman前端,翻译)
- python中如何用dis模块来查看py的汇编代码?
- python 语法常用 lambda
- 黑马程序员——JAVA基础之Vector集合
- MHA学习笔记
- android菜鸟学习笔记2----关于adb
- Red5实现直播
- 04_过滤器Filter_05_Filter解决全站中文乱码问题(POST方式)
- Codeforces 463D Gargari and Permutations
- 使用winform控件注意线程绘制界面冲突
- 【特效】hover向上翻转效果
- IntelliJ IDEA 使用教程
- 【BZOJ3451】Normal
- HashMap的扩容机制---resize()
- P2031 脑力达人之分割字串
- python 什么是位置参数?
热门文章
- ThinkPHP架构(一)-TP原理及路径问题及后台实现实例
- [UWP]使用CompositionLinearGradientBrush实现渐变画笔并制作动画
- css 动画animation基本属性(干货)
- 存储物理页属性的PFN数据库
- SpringBoot 2.0整合阿里云OSS,实现动静分离架构
- 概念理解:boost::asio::io_service
- python编程系列---args与kwargs详解
- 使用 pdf.js 在网页中加载 pdf 文件
- 百万年薪python之路 -- 装饰器
- Java基础(十)接口(interface)