AcWing每日一题--回文平方

https://www.acwing.com/problem/content/1348/

主要考察进位制的转换,数据范围较小,所以并不需要考虑时间复杂度的问题。

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 char get(int a){
 5     if(a<10) return a+'0';
 6     return a-10+'A';
 7 }
 8 string base(int n,int b){
 9     string res="";
10     while(n){
11         res+=get(n%b);
12         n/=b;
13     }
14     reverse(res.begin(),res.end());
15     return res;
16 }
17 bool check(string s){
18     int i=0,j=s.size()-1;
19     while(i<j){
20         if(s[i]!=s[j])
21             return 0;
22         i++,j--;
23     }
24     return 1;
25 }
26 int main(void){
27     int b;
28     cin>>b;
29     for(int i=1;i<=300;i++){
30         string num=base(i*i,b);
31         if(check(num)){
32             cout<<base(i,b)<<" "<<num<<endl;
33         }
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/greenofyu/p/14282043.html