题目描述
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.
输入
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.
输出
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.
样例输入
17
19
0
样例输出
Number 17 is palindrom in basis 2 4 16
Number 19 is not a palindrom
提示
题目大意就是给你一个数找你计算从2-16进制的回文数进制。
AC代码
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
using namespace std;
int save[17];
int te[20];
int main()
{
#ifdef LOCAL
freopen("E://in.txt","r",stdin);
#endif // LOCAL
int n;
while(scanf("%d",&n)&&n)
{
CLR(save,0);
for(int i=2; i<=16; ++i)
{
int a=n,k=0;
CLR(te,0);
while(a)
{
te[k]=a%i;
a/=i;
k++;
}
int flag=0;
for(int j=0; j<k; j++)
{
if(te[j]!=te[k-1-j])
{
flag=1;
break;
}
}
if(flag==0)
{
++save[i];
}
}
int cnt=0;
for(int j=2; j<=16; j++)
{
if(save[j])
{
cnt++;
break;
}
}
if(cnt==0)
printf("Number %d is not a palindrom\n",n);
else
{
printf("Number %d is palindrom in basis",n);
for(int j=2; j<=16; j++)
{
if(save[j])
printf(" %d",j);
}
printf("\n");
}
}
return 0;
}
本文介绍了一个算法问题,即判断一个十进制数在2到16进制中是否为回文数。通过转换数的基础表示并检查其对称性来解决此问题。文章提供了完整的AC代码实现。
1019

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



