Palindromes are numbers that read the same forwards as backwards.The number 12321 is a typical palindrome.
Given a number base B (2 <= B <= 20 base 10), print all theintegers N (1 <= N <= 300 base 10) such that the square of N ispalindromic when expressed in base B; also print the value of thatpalindromic square. Use the letters 'A', 'B', and so on to representthe digits 10, 11, and so on.
Print both the number and its square in base B.
PROGRAM NAME: palsquare
INPUT FORMAT
A single line with B, the base (specified in base 10).SAMPLE INPUT (file palsquare.in)
10
OUTPUT FORMAT
Lines with two integers represented in base B. Thefirst integer is the number whose square is palindromic;the second integer is the square itself.SAMPLE OUTPUT (file palsquare.out)
1 1 2 4 3 9 11 121 22 484 26 676 101 10201 111 12321 121 14641 202 40804 212 44944 264 69696
判断回文子串。
CODE:
/*
ID:sotifis3
LANG:C++
TASK:palsquare
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
map<int, char> m;
int N;
char a[250], b[250];
void init()
{
m[0] = '0'; m[1] = '1'; m[2] = '2'; m[3] = '3'; m[4] = '4';
m[5] = '5'; m[6] = '6'; m[7] = '7'; m[8] = '8'; m[9] = '9';
m[10] = 'A'; m[11] = 'B'; m[12] = 'C'; m[13] = 'D'; m[14] = 'E';
m[15] = 'F'; m[16] = 'G'; m[17] = 'H'; m[18] = 'I'; m[19] = 'J';
m[20] = 'K';
}
int change(int n)
{
memset(a, 0, sizeof(a));
int nn = 0;
while(n > 0){
a[nn++] = m[n%N];
n /= N;
}
return nn;
}
bool judge(int n)
{
int len = change(n);
for(int i = 0, j = len - 1; i <= j; ++i, --j){
if(a[i] != a[j]) return false;
}
return true;
}
void print(int n)
{
int nn = 0;
while(n > 0){
b[nn++] = m[n%N];
n /= N;
}
for(int i = nn - 1; i >= 0; --i){
printf("%c", b[i]);
}
printf(" ");
int ll = strlen(a);
for(int i = ll - 1; i >= 0; --i){
printf("%c", a[i]);
}
printf("\n");
}
int main()
{
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
init();
while(~scanf("%d", &N)){
for(int i = 1; i <= 300; ++i){
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
if(judge(i * i)){
print(i);
}
}
}
return 0;
}