//这道题难度不大,用最简单的枚举就可以了。
//题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=78
#include<iostream>
#include<string>
using namespace std;
bool judge(int);
int m;
int b[1000000];
int a[15];
int main()
{
int i, j, k, n,flag;
while (cin >> m&&m!=0)
{
flag = 0;
j = 0;
for (i = 2; i <= 16; i++)
{
n = m;
int x = 0;
while (n != 0)
{
k = n%i;
n = n / i;
b[x] = k;
x++;
}
if (judge(x))
{
flag = 1;
a[j] = i;
j++;
}
}
if (flag == 1)
{
cout << "Number " << m << " is palindrom in basis";
for (i = 0; i < j; i++)
{
cout << " " << a[i];
}
cout << endl;
}
else cout << "Number " << m << " is not a palindrom" << endl;
}
return 0;
}
bool judge(int x)
{
for (int i = 0; i < x; i++)
{
if (b[i] != b[x - 1 - i])return 0;
}
return 1;
}

本文介绍了一种使用枚举法来判断特定数值在不同进制下是否为回文数的方法,并提供了一段C++代码实现。通过该方法可以找出使给定整数成为回文数的所有可能进制。
1万+

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



