分两步,先做进制转换 transform ( const int number, int base ) ,然后判断是不是回文数 isPalindrom ( vector < int > & v )。
Code
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isPalindrom(vector<int> &v)
{
vector<int> u = v;
reverse(u.begin(), u.end());
if(u == v)
return true;
else
return false;
}
vector<int> transform(const int number, int base)
{
vector<int> v;
int temp = number;
while(temp)
{
int r = temp % base;
v.push_back(r);
temp /= base;
}
reverse(v.begin(), v.end());
return v;
}
int main()
{
while(1)
{
int n;
cin >> n;
if(n == 0)
return 0;
else
{
vector<int> nvec;
vector<int> base;
for(int i = 2; i <= 16; i++)
{
nvec = transform(n, i);
if(isPalindrom(nvec))
base.push_back(i);
}
if(base.size())
{
cout << "Number " << n << " is palindrom in basis";
for(vector<int>::iterator itr = base.begin(); itr != base.end(); itr++)
cout << ' ' << *itr;
cout << endl;
}
else
{
cout << "Number " << n << " is not a palindrom";
cout << endl;
}
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isPalindrom(vector<int> &v)
{
vector<int> u = v;
reverse(u.begin(), u.end());
if(u == v)
return true;
else
return false;
}
vector<int> transform(const int number, int base)
{
vector<int> v;
int temp = number;
while(temp)
{
int r = temp % base;
v.push_back(r);
temp /= base;
}
reverse(v.begin(), v.end());
return v;
}
int main()
{
while(1)
{
int n;
cin >> n;
if(n == 0)
return 0;
else
{
vector<int> nvec;
vector<int> base;
for(int i = 2; i <= 16; i++)
{
nvec = transform(n, i);
if(isPalindrom(nvec))
base.push_back(i);
}
if(base.size())
{
cout << "Number " << n << " is palindrom in basis";
for(vector<int>::iterator itr = base.begin(); itr != base.end(); itr++)
cout << ' ' << *itr;
cout << endl;
}
else
{
cout << "Number " << n << " is not a palindrom";
cout << endl;
}
}
}
return 0;
}