思路:主要是进制的转换
先获得x在b进制下的表示 1-9对应本身 9以上就是对应ABCD....然后使用短除法计算x在b进制下的表示,接着验证回文数,最后调用函数就ok了。
代码:
#include<bits/stdc++.h>
using namespace std;
char get(int x)
{
if(x<=9) return '0'+x;
return x-10+'A';
}
string calc(int x,int b)
{
string res;
while(x)
{
res=get(x%b)+res;
x/=b;
}
return res;
}
bool valid(string s)
{
int i=0,j=s.size()-1;
while(i<j) if(s[i++]!=s[j--]) return false;
return true;
}
int main()
{
int b;
cin>>b;
for(int i=1;i<=300;i++)
{
string j=calc(i*i,b);
if(valid(j)) cout<<calc(i,b)<<' '<<j<<endl;
}
return 0;
}