Description
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
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 mes
Source:#include<iostream>
using namespace std;
int n;
int Palindrom(int x)
...{
int temp[100];
int i=0,j=0;
int t;
t=n;
while(t!=0)
...{
temp[j++]=t%x;
t=t/x;
}
j--;
while(i<j)
...{
if(temp[i++]!=temp[j--]) return 0;
}
return 1;
}
int main()
...{
// freopen("fjnu_1827.in","r",stdin);
int m,a[20];
int i;
cin>>n;
while(n!=0)
...{
m=0;
for(i=2;i<=16;i++)
...{
if(Palindrom(i)) a[++m]=i;
}
if(m>0)
...{
cout<<"Number "<<n<<" is palindrom in basis ";
int j;
for(j=1;j<=m-1;j++)
cout<<a[j]<<" ";
cout<<a[j]<<endl;
}
else
cout<<"Number "<<n<<" is not a palindrom"<<endl;
cin>>n;
}
return 0;
}

sage 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
KEY:先进制转化,然后判断;
本文介绍了一种算法,该算法能够检测一个给定的整数是否为2到16进制之间的任何一种进制下的回文数。通过将数字从十进制转换为指定进制,并检查其是否为回文数来实现这一目标。

408

被折叠的 条评论
为什么被折叠?



