【USACO1.2】【Luogu1206】回文平方数 Palindromic Squares(枚举,构造函数,进制)

problem

  • 给出一个进制B
  • 求1~300内有哪些十进制数的平方,在B进制下是回文数。
  • 输出这些数的B进制表示

solution

  • C++构造函数了解一下

codes

#include<iostream>
#include<string>
using namespace std;
const string s = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int b;
struct node{
    int a[20], len;
    node(int x){len=0;while(x)a[++len]=x%b,x/=b;}//转为B进制
    void out(){for(int i=len;i>=1;i--)cout<<s[a[i]];}
    bool tf(){for(int i=1;i<=len;i++)if(a[i]!=a[len-i+1])return false; return true;}
};
int main(){
    cin>>b;
    for(int i = 1; i <= 300; i++){
        node n(i*i);
        if(n.tf()){
            node m(i); m.out(); cout<<' ';
            n.out(); cout<<'\n';
        }
    }
    return 0;
}
posted @ 2018-07-28 15:47  gwj1139177410  阅读(192)  评论(0编辑  收藏  举报
选择