Palindrom Numbers

来源:互联网 发布:软件项目开发阶段 编辑:程序博客网 时间:2024/04/27 22:28

题目描述

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-8typedef 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