顽强的小白
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.
题目解析
这是一道大数处理的题目,数据最多可能会有1000位,因此适合采用数组形式其实,只要思路清晰,做起来不难。
我早先写的时候思路可能不太清晰,第三个case就是过不去,不想改也找不到错在哪里,搁置了这么久,从头开始写的,调试了几次后就通过了,调试过程中发现的最大错误就是数据的位数siza问题。
代码实现
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=1005;
int cnt=0,size1=0,size2;
int a[maxn],b[maxn],sum[maxn];
void print(int a[],int size){
for(int i=0;i<size;++i){
printf("%d",a[i]);
}
}void add(int size){
int s,yu=0;
for(int i=size-1;i>=0;--i){
s=a[i]+b[i]+yu;
sum[i+1]=s%10;
yu=s/10;
}
if(yu!=0){
sum[0]=yu;
size2=size+1;
}else{
for(int i=0;i<size;++i){
sum[i]=sum[i+1];
}
}
}
void copy(int size){
for(int i=0;i<size;++i){
a[i]=sum[i];
}
}
void reverse(int size){
for(int i=0;i<size;++i){
b[i]=a[size-1-i];
}
}
bool judge(int size){
for(int i=0;i<size/2;++i){
if(a[i]!=a[size-1-i]){
return false;
}
}
return true;
}
int main(){
char num[maxn];
scanf("%s",num);
for(int i=0;i<strlen(num);++i){
a[i]=num[i]-'0';
}
size1=size2=strlen(num);
while(judge(size1)==false&&cnt<10){
reverse(size1);
add(size1);
print(a,size1);
printf(" + ");
print(b,size1);
printf(" = ");
print(sum,size2);
printf("\n");
copy(size2);
cnt++;
size1=size2;
}
if(cnt<10){
print(a,size1);
printf(" is a palindromic number.\n");
}else{
printf("Not found in 10 iterations.\n");
}
}