1. 程式人生 > >【USACO】palsquare

【USACO】palsquare

/*
ID: ahshenb1
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

string base = "0123456789ABCDEFGHIJ";

string trans(int i,int n)
{
    string res;
    while(i>=n){
           // cout<<i%n<<endl;
        res += base[i%n];
        i = i/n;
    }
    res+=base[i];
    return res;
}
bool check(string res){
    int s =res.size();
    bool result =true;
    for(int i=0;i<s/2;++i){
        if(res[i]!=res[s-i-1])
        {
            result = false;
            break;
        }
    }
    return result;
}
string reverse(string tmp){
    string t = tmp;
    for(size_t i=0;i <t.size()/2;++i){
        char x = t[i];
        t[i] = t[t.size()-i-1];
        t[t.size()-i-1] = x;
    }
    return t;
}
int main()
{
    ifstream fin("palsquare.in");
    ofstream fout("palsquare.out");
    int n;fin>>n;
    string tmp;
    for(int i=1;i<=300;++i){
        tmp = trans(i*i,n);
        if(check(tmp)){
            fout<<reverse(trans(i,n))<<" "<<tmp<<endl;
        }
    }
//    cout<<check(trans(300,8));

    //cout<<trans(101*101,10);

    return 0;
}