PTA甲级——1136 A Delayed Palindrome (20 分)

爷,独闯天下 提交于 2019-12-05 07:43:06

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ with 0≤a​i​​<10 for all i and a​k​​>0. Then N is palindromic if and only if a​i​​=a​k−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函数
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!