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.
代碼如下:
/*1136
大數加法
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ori_Num[1005],rev_Num[1005],sum_Num[1005];//ori_Num爲原始數據,rev_Num爲反轉數據,sum_Num爲和
int carry,ori_Len,sum_Len,is_PalNum=1,iteration=0;//carry爲進位,ori_Len爲原始數據長度,sum_Len爲和長度,is_PalNum標記是否找到迴文數,iteration爲迭代次數
memset(ori_Num,0,sizeof(ori_Num));
memset(rev_Num,0,sizeof(rev_Num));
memset(sum_Num,0,sizeof(sum_Num));
scanf("%s",ori_Num);
ori_Len=strlen(ori_Num);
//判斷ori_Num是否爲迴文數
for(int i=0; i<ori_Len; i++)
{
if(ori_Num[i]!=ori_Num[ori_Len-1-i])
{
is_PalNum=0;
break;
}
}
if(is_PalNum)
{
for(int i=0; i<ori_Len; i++)
printf("%c",ori_Num[i]);
printf(" is a palindromic number.");
return 0;
}
//找到迴文數或者迭代次數到達10次時跳出循環
while(is_PalNum==0&&iteration<10)
{
ori_Len=strlen(ori_Num);
is_PalNum=1;//初始化默認爲迴文數
carry=0;
//爲反轉數據rev_Num賦值
for(int i=0; i<ori_Len; i++)
rev_Num[i]=ori_Num[ori_Len-1-i];
//從個位開始將原始數據的真值與反轉數據的真值逐位相加+前一位進位對10取餘得到當前位數值,除10更新進位
for(int i=ori_Len-1; i>=0; i--)
{
sum_Num[i+1]=(ori_Num[i]-'0'+rev_Num[i]-'0'+carry)%10+'0';
carry=(ori_Num[i]-'0'+rev_Num[i]-'0'+carry)/10;
}
//根據首位是否爲0對和的格式進行調整
if(!carry)
{
sum_Len=ori_Len;
for(int i=0; i<sum_Len; i++)
sum_Num[i]=sum_Num[i+1];
}
else
{
sum_Len=ori_Len+1;
sum_Num[0]=carry+'0';
}
//判斷和是否爲迴文數
for(int i=0; i<sum_Len; i++)
{
if(sum_Num[i]!=sum_Num[sum_Len-1-i])
{
is_PalNum=0;
break;
}
}
for(int i=0; i<ori_Len; i++)
printf("%c",ori_Num[i]);
printf(" + ");
for(int i=0; i<ori_Len; i++)
printf("%c",rev_Num[i]);
printf(" = ");
for(int i=0; i<sum_Len; i++)
printf("%c",sum_Num[i]);
printf("\n");
strncpy(ori_Num,sum_Num,sum_Len);
iteration++;
}
if(is_PalNum)
{
for(int i=0; i<sum_Len; i++)
printf("%c",sum_Num[i]);
printf(" is a palindromic number.");
}
else
printf("Not found in 10 iterations.");
return 0;
}
這道題剛開始數組開了1001、1001、1002,結果運行錯誤,我表示無法理解爲什麼會數組溢出呢?先改成1005
還有一個坑是這道題需要先對輸入的原始數據A判斷是否爲迴文數,如果是的話則不需要進行下面的加法運算,巨坑
學習改進
https://blog.csdn.net/wx960421/article/details/86596231
//字符串反轉
reverse(s.begin(), s.end());//使用algorithm中的reverse函數
strrev(s);//使用string.h中的strrev函數