ACM上的一道题 Palindrom Numbers
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
个人代码如下:
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
string v;
int main()
{
int n,i,j;
int reverse(string s);
string s(int n,int i);
cin>>n;
while(n!=0){
j=0;
for(i=2;i<=16;i++){
v="";
if(reverse(s(n,i))!=0){
j=1;
break;
}
}
if(j==1){
cout<<"Number "<<n<<" is palindrom in basis" ;
for(i=2;i<=16;i++){
v="";
if(reverse( s(n,i) )!=0){
cout<<" "<<i ;
}
}
}
else{
cout<<"Number "<<n<<" is not a palindrom";
}
cout<<endl;
cin>>n;
}
return 0;
}
int reverse(string s)
{
if(equal(s.begin(),s.end(),s.rbegin()))
return 1;
else
return 0;
}
string s(int n,int i)
{
string temp;
int mod;
while(n>0){
mod=n%i;
n=n/i;
if(mod<=9){
temp='0'+mod;
v=temp+v;
}
else{
temp='a'+mod-10;
v=temp+v;
}
}
return v;
}
这是鄙人学C++后写的第一次作业 也算有点意义 也就传一下 中间用迭代器判断回文觉得是C++胜过C的一个突出体现