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 (<= 1010 ) 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;
}

如果對您有幫助,幫忙點個小拇指唄~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章