Description
回文数是指从左向右念和从右向左念都一样的数。如12321就是一个典型的回文数。
给定一个进制B(2<=B<=20,由十进制表示),输出所有的大于等于1小于等于300(十进制下)且它的平方用B进制表示时是回文数的数。用’A’,’B’……表示10,11等等
Input
共一行,一个单独的整数B(B用十进制表示)。
Output
每行两个B进制的符合要求的数字,第二个数是第一个数的平方,且第二个数是回文数。
Sample Input
10
Sample Output
1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696
直接进制转换然后判断
#include <iostream>
#include <cstdio>
#include <cstring>
#define SIZE 210
using namespace std;
int a[SIZE], temp[SIZE], n, len, len2;
bool check(void) // 判断回文
{
int i;
for (i = 1; i <= len2; ++i)
{
if (temp[i] != temp[len2-i+1])
{
return false;
}
}
return true;
}
void mul(void) // 高精度乘
{
int i, j;
len2 = len << 1;
memset(temp, 0, sizeof (temp));
for (i = 1; i <= len; ++i)
{
for (j = 1; j <= len; ++j)
{
temp[i+j-1] += a[i] * a[j];
}
}
for (i = 1; i <= len2; ++i)
{
if (temp[i] >= n)
{
temp[i+1] += temp[i] / n;
temp[i] %= n;
}
}
while (!temp[len2])
{
--len2;
}
return;
}
int main(void)
{
int i, j, temp;
scanf("%d", &n);
for (i = 1; i <= 300; ++i)
{
memset(a, 0, sizeof (a));
temp = i;
len = 0;
while (temp) // 进制转换
{
a[++len] = temp % n;
temp /= n;
}
mul();
if (check())
{
for (j = len; j; --j)
{
if (a[j] > 9)
{
printf("%c", 'A' + a[j] - 10);
}
else
{
printf("%d", a[j]);
}
}
printf(" ");
for (j = len2; j; --j)
{
if (::temp[j] > 9)
{
printf("%c", 'A' + ::temp[j] - 10);
}
else
{
printf("%d", ::temp[j]);
}
}
printf("\n");
}
}
return 0;
}