ZOJ Problem Set - 1078 Palindrom Numbers

Palindrom Numbers

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Statement of the Problem

We say that a number is a palindrom if it is the sane when read from left to right or from right to left. For example, the number 75457 is a palindrom.

Of course, the property depends on the basis in which is number is represented. The number 17 is not a palindrom in base 10, but its representation in base 2 (10001) is a palindrom.

The objective of this problem is to verify if a set of given numbers are palindroms in any basis from 2 to 16.

Input Format

Several integer numbers comprise the input. Each number 0 < n < 50000 is given in decimal basis in a separate line. The input ends with a zero.

Output Format

Your program must print the message Number i is palindrom in basis where I is the given number, followed by the basis where the representation of the number is a palindrom. If the number is not a palindrom in any basis between 2 and 16, your program must print the message Number i is not palindrom.

Sample Input

17
19
0

Sample Output

Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom


Source:  South America 2001





分析:
题意:
有多组测试数据。每组测试数据一个数n(0<n<50000),要求判断n在2~16进制哪些进制下是回文串,并输出全部符合要求的进制数,如果不存在符合条件的进制,输出no。



简单题。对于每个输入,一个for循环判断2~16进制15种情况,每种情况再判断是否是回文串就可以,然后存储回文串的信息。




ac代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[20];
bool b[18];
int main()
{
    int n,i,j,k;
    while(scanf("%d",&n)&&n)
    {
        for(i=2;i<=16;i++)
        b[i]=false;
        for(i=2;i<=16;i++)//2~16进制
        {
            int m=n;
            int j=0;
            while(m)
            {
                a[++j]=m%i;
                m/=i;
            }
            for(k=1;k<=j/2;k++)//判断是不是回文串
            {
                if(a[k]!=a[j-k+1])
                break;
            }
            if(k>j/2)
            b[i]=true;
        }
        for(i=2;i<=16;i++)
        if(b[i]) break;
        if(i>16)
        printf("Number %d is not a palindrom\n",n);
        else
        {
            printf("Number %d is palindrom in basis",n);
            for(i=2;i<=16;i++)
            if(b[i])
            printf(" %d",i);
            printf("\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值