PAT(甲)1024 Palindromic Number (25)(详解)

1024 Palindromic Number (25)(25 分)

题目描述:

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.


  • 输入格式
    Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 10 10 ) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

  • 输出格式
    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.


题目大意:
题目就是想问你,一个数经过有限步的自己与自己的逆序相加的结果是否有可能出现相加结果为回文数,如果有就输出这个回文数,并且输出步数,没有输出最后的结果,并输出K

解题方法:
这里主要考察的是大数加法,这个比较简单,用字符型数组做就可以,如果还有疑惑的话直接可以看我的代码,里面注释很详细。


易错点:
1. 在保存大数结果的时候,sum要开的稍微大一点,不然会乱码(暂不知晓原因)


程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char N[200], out[200];

bool isPalin(char ch[])
{
    for (int i = 0; i < strlen(ch)/2; i++)  /* 判断是否是回文数 */
        if (ch[i] != ch[strlen(ch)-1-i])    /* 如出现不同,则直接返回false */
            return false;
    return true;
}

char* Add(char ch[])    /* 大数加法 */
{   
    int len = strlen(ch);
    char temp[len];     /* 原数组的逆序数 */
    char *Sum;
    Sum = new char[len + 10];   /* 如果这里写len+1,当ch所表示的数字比较大就会乱码 */ 
    for (int i = 0; i < len; i++)
    {
        Sum[i] = '0';   /* 初始化 */
        temp[i] = ch[len-i-1];
    }
    Sum[len] = '0';
    for (int i = len; i >= 1; i--)  /* 计算结果 */
    {
        Sum[i] += ch[i-1] + temp[i-1] - 2*'0';
        if (Sum[i] > '9') 
        {   /* 如果结果超过'9'也就是至少为10,则要进位 */
            Sum[i] -= 10;
            Sum[i-1] = '1';
        }
    }
    if (Sum[0] == '0')
        Sum += 1;   /* 如果第一位是0即相加的结果没有超过之前的位数 */
    return Sum;
}

int main(int argc, char const *argv[])
{
    int K, flag = 0;
    scanf("%s %d", N, &K);
    if (isPalin(N))
        printf("%s\n%d", N, 0);
    else
    {
        for (int i = 1; i <= K; i++)
        {
            strcpy(out, Add(N));
            if (isPalin(out))
            {
                printf("%s\n%d", out , i);
                flag = 1;
                break;
            }
            strcpy(N, out);
        }
        if (flag == 0)
            printf("%s\n%d", out, K);
    }
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

猜你喜欢

转载自blog.csdn.net/invokar/article/details/80718481