题目选自usaco
方法 模b取余法,与传统二进制求法类似 只不过把模2改成模b
注意 取完模后的结果是倒叙的 需要将其反过来才是b进制的结果。
判断是否回文双指针扫描即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 300;
int b;
char get(int c){
if(c<=9) return c + '0';
else return 'A'+c-10;
}
string base(int t,int b){
string res;
while(t){
res+=get(t%b);
t/=b;
}
reverse(res.begin(),res.end());
return res;
}
bool check(string s){
for(int i=0,j=s.size()-1;i<j;i++,j--){
if(s[i]!=s[j]){
return false;
}
}
return true;
}
int main(){
cin>>b;
for(int i=1;i<=N;i++){
string num = base(i*i,b);
if(check(num)){
cout<<base(i,b)<<" "<<num<<endl;
}
}
return 0;
}