PAT(甲)1019 General Palindromic Number (20)(詳解)

1019 General Palindromic Number (20)(20 分)

題目描述:

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.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N > 0 in base b >= 2, where it is written in standard notation with k+1 digits ai as the sum of (aibi) for i from 0 to k. Here, as usual, 0 <= ai < b for all i and ak is non-zero. Then N is palindromic if and only if ai = ak-i for all i. Zero is written 0 in any base and is also palindromic by definition.
Given any non-negative decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.


  • 輸入格式
    Each input file contains one test case. Each case consists of two non-negative numbers N and b, where 0 <= N <= 109 is the decimal number and 2 <= b <= 109 is the base. The numbers are separated by a space.

  • 輸出格式
    For each test case, first print in one line “Yes” if N is a palindromic number in base b, or “No” if not. Then in the next line, print N as the number in base b in the form “ak ak-1 … a0”. Notice that there must be no extra space at the end of output.


題目大意:
題目大意就是判斷迴文數,主要考察進制轉換

解題方法:
用取餘的方法可以將十進制數字轉爲任意進制的數(不過這樣做的話,輸出的進制碼是反着來的,不過不影響),然後進行判斷就好


易錯點:
1. 如果用char型數組接收進制轉換結果,建議在末尾添上’\0’
2. 我在這裏用char做,發現始終有兩個樣例通不過,而用int卻沒這個問題,下面我會把兩個都貼上來,望大佬能幫我把錯誤的地方改過來,不勝感激!
3. 第2個錯誤已經找到原因:原因是因爲進制範圍爲[2-109 ],而8位ASCII碼最大值爲255,因此,如果對在對取餘結果+’0’的時候就會越界,因此會錯誤。


程序:
AC版本

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int N, b, idx = 0, A[100], flag = 0;
    scanf("%d %d", &N, &b);
    while (N > 0)
    {   /* 獲得數字N的b進製表示 */
        A[idx++] = N % b;
        N /= b;
    }
    for (int i = 0, j = idx-1; j >= i; i++, j--)
        if (A[i] != A[j])   /* 相應位置進行比較,出現不同則進入判定 */
        {
            flag = 1;
            break;
        }
    if (idx == 1 || idx == 0 || flag == 0)
    {
        printf("Yes\n");
        if (idx == 0)
        {   /* 如果輸入是0則單獨考慮 */
            printf("0\n");
            return 0;
        }

    }
    else if (flag == 1) /* 如果不是迴文數 */
        printf("No\n");
    for (int i = idx - 1; i >= 0; i--)
    {   /* 按格式輸出 */
        if (i == idx - 1)
            printf("%d", A[i]);
        else
            printf(" %d", A[i]);
    }
    return 0;
}

WA版本

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int N, b, idx = 0, flag = 0;
    char ch[101];
    scanf("%d %d", &N, &b);
    while (N > 0)
    {
        ch[idx++] = N % b + '0';
        N /= b;
    }
    ch[idx] = '\0';
    for (int i = 0, j = idx-1; j >= i; i++, j--)
        if (ch[i] != ch[j])
        {
            flag = 1;
            break;
        }

    if (idx == 1 || idx == 0 || flag == 0)
    {
        printf("Yes\n");
        if (idx == 0)
        {
            printf("0\n");
            return 0;
        }

    }
    else if (flag == 1)
        printf("No\n");
    for (int i = idx - 1; i >= 0; i--)
    {
        if (i == idx - 1)
            printf("%c", ch[i]);
        else
            printf(" %c", ch[i]);
    }

    return 0;
}

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

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