题目请点我
题意:
简单题,找出1~300以内平方转换为B进制为回文数的数,并输出。
代码实现:
/*
ID: eashion
LANG: C++
TASK: palsquare
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAX 1000
using namespace std;
int B;
int len;
char res[MAX];
bool test(int x);
void print(int x);
char base[25] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
int main()
{
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
scanf("%d",&B);
for( int i = 1; i <= 300; i++ ){
int tmp = i*i;
if( test(tmp) ){
print(i);
}
}
return 0;
}
bool test(int x){
len = 0;
memset(res,0,sizeof(res));
while( x ){
res[len] = base[x%B];
x /= B;
len++;
}
for( int i = 0; i < len; i++ ){
if( res[i] != res[len-1-i] ){
return false;
}
}
return true;
}
void print(int x){
int pos = 0;
char tmp[MAX];
memset(tmp,0,sizeof(tmp));
while( x ){
tmp[pos] = base[x%B];
x /= B;
pos++;
}
for( int i = pos-1; i >= 0; i-- ){
printf("%c",tmp[i]);
}
printf(" ");
for( int i = len-1; i >= 0; i-- ){
printf("%c",res[i]);
}
printf("\n");
return ;
}