We say that a number is a palindrom if it is the same 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 the 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
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
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. The leftmost digit of a number in any basis must be nonzero. 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 palindrom
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[17];
int b[20];
int f1;//判断有无成立的进制;
void zj(int n,int c){//转化进制;c为要转化为几进制
memset(b,0,sizeof(b));
int i;//代表转化为c进制后有多少位数
for(i=0;;i++){
int e=n%c;
b[i]=e;
n/=c;
if(n==0){
i++;
break;
}
}
int r;//找中间个数
if(i&1){
r=(i-1)/2;
}
else{
r=i/2;
}
int f=1;
for(int w=0;w<r;w++){
if(b[w]!=b[i-1-w]){
f=0;
break;
}
}
if(f==1){
f1++;
a[c]=1;
return;
}
return;
}
int main(){
for(;;){
int n;
cin>>n;
if(n==0){
break;
}
else{
f1=0;
memset(a,0,sizeof(a));
for(int i=2;i<17;i++){
zj(n,i);
}
if(f1==0){
cout<<"Number "<<n<<" is not palindrom"<<endl;
}
else{
cout<<"Number "<<n<<" is palindrom in basis";
for(int i=2;i<17;i++){
if(a[i]==1){
cout<<" "<<i;
}
}
cout<<endl;}
}
}
}