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)
10OUTPUT 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. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!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
这题如果翻译的比较奇葩,就去洛谷看一看翻译
就是在b进制下,300以内的数的平方如果是回文数就输出
这题两个重要点:
1.进制转换
2.判断回文
这两个可以结合到一个函数,最后输出再来一个函数
因为输出时需要有b进制,所以再来个函数转换
/*
ID:liusiyu3
TASK:palsquare
LANG:C++
*/
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
# define int long long
char a1[100];
char a[100];
int b;
int j=0;
char sum[100]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
void out(int a,int b) {
int j=0;
while(a){
j++;
a1[j]=a%b+'0';
if(a1[j]>'9'){
a1[j]=a1[j]-'9'+'A'-1;
}
a/=b;
}
for(int i=j;i>=1;i--){
cout<<a1[i];
}
return ;
}
bool jz(int x,int n){
j=0;
memset(a,0,sizeof(a));
while(x){
j++;
if (x%n>=10){
a[j]=sum[x%n];
//cout<<sum[x%n]<<" ";
}else{
a[j]=char(x%n+'0');
//cout<<char(x%n+'0')<<"\n";
}
x/=n;
}
//cout<<j<<" ";
for (int i=1;i<=j;i++){
if (a[i]!=a[j-i+1]){
return false;
}
}
return true;
}
signed main(){
freopen("palsquare.in","r",stdin);
freopen("palsquare.out","w",stdout);
scanf("%lld",&b);
for (int i=1;i<=300;i++){
if (jz(i*i,b)){
//jz(i,b);
out(i,b);
printf(" ");
out(i*i,b);
printf("\n");
}
}
fclose(stdin);
fclose(stdout);
return 0;
}