Rob Kolstad
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 the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the 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. The first 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
题解:直接将模拟,1.将数值转换为相应的进制,再将其转化为字符,注意进制,判断是否为回文数。
AC code:
/*ID: *****
PROG: palsquare
LANG: C++
*/
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
char num[]={"0123456789ABCDEFGHIJK"};
void change(int n,char *s, int b){ //将n转换为b进制,再转换为字符。
int k=0,i,str[30];
while(n){
str[k++]=num[n%b];
n/=b;
}
str[k]=0;
for( i=0;i<k;i++)
s[i]=str[k-1-i];
s[k]=0;
}
int palsquare(char str[],int l){ //判断是否为回文数
int i;
for(i=0;i<l;i++)
if(str[i]!=str[l-i-1])
return 0;
return 1;
}
int main(){
int t,i,l;
char s1[30],s2[30];
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
cin>>t;
for(i=1;i<=300;i++){
change(i*i,s1,t);
l=strlen(s1);
if(palsquare(s1,l)){
change(i,s2,t);
cout<<s2<<" "<<s1<<endl;
}
}
return 0;
}