Palindrom Numbers

题目描述

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.

输入

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.

输出

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.

样例输入

17
19
0

样例输出

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

提示

题目大意就是给你一个数找你计算从2-16进制的回文数进制。
AC代码

//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
using namespace std;
int save[17];
int te[20];


int main()
{
#ifdef LOCAL
    freopen("E://in.txt","r",stdin);
#endif // LOCAL
    int n;
    while(scanf("%d",&n)&&n)
    {
        CLR(save,0);
        for(int i=2; i<=16; ++i)
        {
            int a=n,k=0;
            CLR(te,0);
            while(a)
            {
                te[k]=a%i;
                a/=i;
                k++;
            }
            int flag=0;
            for(int j=0; j<k; j++)
            {
                if(te[j]!=te[k-1-j])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                ++save[i];
            }
        }
        int cnt=0;
        for(int j=2; j<=16; j++)
        {
            if(save[j])
            {
                cnt++;
                break;
            }
        }
        if(cnt==0)
            printf("Number %d is not a palindrom\n",n);
        else
        {
            printf("Number %d is palindrom in basis",n);
            for(int j=2; j<=16; j++)
            {
                if(save[j])
                    printf(" %d",j);
            }
            printf("\n");
        }

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值