1.2.4 Palindromic Squares 回文平方数

1.2.4 Palindromic Squares 回文平方数

``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<stdio.h>
#include<string.h>
int change(int n,int base,char str[])
{
int i=0;
while(n){
str[i]=n%base;
if(str[i]<10)
str[i]+='0';
else
str[i]=str[i]-10+'A';
i++;
n=n/base;
}
str[i]=0;
return 1;
}
int judge(char str[])
{
int n=strlen(str),i;
for(i=0;i<n/2;i++)
if(str[i]!=str[n-i-1])
return 0;
return 1;
}
int main()
{
int n;
while(scanf("%d",&n)==1){
int i;
char str[1000]={0},c[1000]={0};
for(i=1;i<=300;i++)
if(change(i,n,c)&&change(i*i,n,str)&&judge(str)){
int len=strlen(c);
while(len--)
printf("%c",c[len]);
printf(" %s\n",str);
}
}
return 0;
}``````

``````#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;

int b,n;
int i,j,k,r;
int a[50],a2[50];

void zhuanhuan(int a[50],int n)
{
int m1,c1;
c1=n;
j=0;
while(c1>0)
{
m1=n%b;
//cout<<"haha..."<<endl;
a[j]=m1;
j++;
c1=n/b;
n=n/b;
}
}

int main()
{
cin>>b;
for(k=1;k<=300;k++)
{
for(i=0;i<50;i++)
{
a[i]=-1;
a2[i]=-1;
}
int bool1=1;
n=k*k;
zhuanhuan(a,n);
int len=0;
for(int t=0;a[t]>=0;t++)
len++;
//cout<<" len= "<<len<<endl;
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-1-i])
{
bool1=0;
break;
}
}
if(bool1==1)
{
zhuanhuan(a2,k);
int len2=0;
for(int t1=0;a2[t1]>=0;t1++)
len2++;
for(int t2=len2-1;t2>=0;t2--)
{
if(a2[t2]>=10)
{
switch(a2[t2])
{
case 10:cout<<"A";
break;
case 11:cout<<"B";
break;
case 12:cout<<"C";
break;
case 13:cout<<"D";
break;
case 14:cout<<"E";
break;
case 15:cout<<"F";
break;
case 16:cout<<"G";
break;
case 17:cout<<"H";
break;
case 18:cout<<"I";
break;
case 19:cout<<"J";
break;
}
}
else
cout<<a2[t2];

}
cout<<" ";
for(r=0;r<len;r++)
{
if(a[r]>=10)
{
switch(a[r])
{
case 10:cout<<"A";
break;
case 11:cout<<"B";
break;
case 12:cout<<"C";
break;
case 13:cout<<"D";
break;
case 14:cout<<"E";
break;
case 15:cout<<"F";
break;
case 16:cout<<"G";
break;
case 17:cout<<"H";
break;
case 18:cout<<"I";
break;
case 19:cout<<"J";
break;
}
}
else
cout<<a[r];
}
cout<<endl;
}
}

return 0;
}``````