题解 P1206 【[USACO1.2]回文平方数 Palindromic Squares】

· · 题解

一开始不知道处理A、B、C,然后又看了看题...

#include<bits/stdc++.h>
using namespace std;
int n;//要求用ABCD来表示10、11、12.。。。。。。
inline string change(int decs){//输入十进制数
    string x;
    x.clear();
    stack<short> ns;//翻转一下结果
    while(decs){
        ns.push(decs%n);
        decs/=n;
    }
    short ws=ns.size();
    for(short i=1;i<=ws;i++){
        short y=ns.top();
        ns.pop();
        if(y<10) x+=(y+'0');
        else x+=(y-10+'A');//方便直接输出
    }
    return x;
}
inline bool check(const string &x){//加上const和&更保险
    short i=0,j=x.size()-1;
    while(i<j)
        if(x[i++]!=x[j--])
                       return false;//判断回文
    return true;
}
int main (){
    scanf("%d",&n);
    for(int i=1;i<=300;i++){
        int inp=i*i;//直接算平方
        string outp=change(inp);//转换
        if(check(outp))//检查
            cout<<change(i)<<" "<<outp<<endl;
    }
    return 0;
}