zoj 1078 Palindrom Numbers

来源:互联网 发布:淘宝商城外包 编辑:程序博客网 时间:2024/04/26 12:32
Problem Description
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

题意:给你一个十进制数,求他的2-16进制,判断是否为回文

#include <iostream>#include <cstring>#include <cstdio>int a[50000],b[50000],c[50000];using namespace std;int is_p(int n){    int flag=1;    for(int i=n,j=0;i>=0;i--,j++)    {        b[j]=a[i];       // cout<<b[j]<<" ";    }    //cout<<endl;    for(int i=0;i<n;i++)    {        if(a[i]!=b[i])        {            flag=0;        }    }    return flag;}int f(int n,int m){    memset(a,sizeof(a),0);    memset(b,sizeof(b),0);    int d;    for(int i=0;n!=0;i++)    {        a[i]=n%m;        n=n/m;        d=i;       // cout<<a[i]<<" ";    }   // cout<<endl;    if(is_p(d))    return 1;    return 0;}int main(){    int n;    while(cin>>n)    {        if(n==0) break;        memset(c,sizeof(c),0);        int flag=0,k=0;        for(int i=2;i<=16;i++)        {            //cout<<i<<endl;            if(f(n,i))            {                flag=1;                c[k++]=i;            }        }        if(!flag)           printf("Number %d is not a palindrom\n",n);        else        {             printf("Number %d is palindrom in basis",n);            for(int i=0;i<k;i++)                cout<<" "<<c[i];            cout<<endl;        }    }    return 0;}


0 0