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 <= 10 9 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- 10 9 ],而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;
}

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

猜你喜欢

转载自blog.csdn.net/invokar/article/details/80604951
今日推荐