#include <iostream>
#include <stack>
#include <vector>
using namespace std;
bool IsPalindrom(stack<int>& s,const vector<int>& v)
{//判断是否是"回文"
int index = 0;
while(!s.empty())
{
if(s.top()!=v[index++])
{
return false;
}
s.pop();
}
return true;
}
bool DividN(int num,int n)
{//n进制除法
stack<int> s1;
vector<int> v1;
int tmp;
while (num!=0)
{
tmp = num%n;
s1.push(tmp);
v1.push_back(tmp);
num = num/n;
}
return IsPalindrom(s1,v1);
}
int main(void)
{
int n,i;
while(cin>>n&&n!=0)
{
bool isFirst = true;//第一个是回文的进制
for (i=2;i<=16;++i)
{
if (DividN(n,i))
{
if (isFirst==true)
{
cout<<"Number "<<n<<" is palindrom in basis "<<i;
isFirst = false;
}
else
{
cout<<" "<<i;
}
}
}
if (isFirst==true)
{//不是回文
cout<<"Number "<<n<<" is not a palindrom";
}
cout<<endl;
}
return 0;
}
复制代码
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/10/30/1323233.html,如需转载请自行联系原作者