ZOJ1078 Palindrom Numbers
#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;
}
#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;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2008-10-30 18:09 Phinecos(洞庭散人) 阅读(424) 评论(0) 编辑 收藏 举报