(Input: palsquar.in, Output: palsquar.out)
时间限制: 1 s 空间限制: 256 MB
题目描述
回文数是指从左向右念和从右像做念都一样的数。如12321就是一个典型的回文数。
给定一个进制B(2<=B<=20十进制),输出所有的大于等于1小于等于300且它的平方用B进制表示时是回文数的数。用’A’,’B’……表示10,11等等。
输入
从文件 palsquar.in
中读入数据。
共一行,一个单独的整数B(B用十进制表示)。
输出
输出到文件 palsquar.out
中。
每行两个数字,第二个数是第一个数的平方,且第二个数是回文数。(注意:这两个数都应该在B那个进制下)
样例输入 复制
10
样例输出 复制
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<cstdio>
#include<cstring>
char res[30000];
int len;
void turn(int x,int y){
if(x<y){
if(x>9){
len=0;
res[0]='A'+x-10;
len++;
res[1]='\0' ;
return;
}
else{
len=0;
res[0]='1'+x-1;
len++;
res[1]='\0';
return;
}
}
turn(x/y,y);
if(x%y>=10){
res[len]='A'+x%y-10;
}
else{
res[len]='0'+x%y;
}
len++;
res[len]='\0';
}
int main(){
freopen("palsquar.in","r",stdin);
freopen("palsquar.out","w",stdout);
int b;
bool bj=false;
scanf("%d",&b);
for(int i=1;i<=300;i++){
turn(i*i,b);
bj=false;
for(int j=0;j<strlen(res)/2+strlen(res)%2;j++){
if(res[j]!=res[strlen(res)-j-1]){
bj=true;
break;
}
}
if(bj==false){
turn(i,b);
printf("%s ",res);
turn(i*i,b);
printf("%s\n",res);
}
}
}
运行语言:C++