진법변환에 대한 개념을 묻는 문제.
string fromDecimal(int n, int b)
{
string result;
while(n>0)
{
if(n%b >= 10) result += 'A'+(n%b)%10;
else result += '0' + (n%b);
n/=b;
}
reverse(ALL(result));
return result;
}
bool is_pal(string s)
{
int n = s.size();
REP(i, n/2)
{
if( s[i] != s[n-1-i] ) return false;
}
return true;
}
int main() {
ofstream fout ("palsquare.out");
ifstream fin ("palsquare.in");
int n;
fin >> n;
for(int i=1; i<=300; i++)
{
string s = fromDecimal(i, n);
string s1 = fromDecimal(i*i, n);
if( is_pal(s1) ) fout << s << " "<< s1 <<endl;
}
return 0;
}
string fromDecimal(int n, int b)
{
string result;
while(n>0)
{
if(n%b >= 10) result += 'A'+(n%b)%10;
else result += '0' + (n%b);
n/=b;
}
reverse(ALL(result));
return result;
}
bool is_pal(string s)
{
int n = s.size();
REP(i, n/2)
{
if( s[i] != s[n-1-i] ) return false;
}
return true;
}
int main() {
ofstream fout ("palsquare.out");
ifstream fin ("palsquare.in");
int n;
fin >> n;
for(int i=1; i<=300; i++)
{
string s = fromDecimal(i, n);
string s1 = fromDecimal(i*i, n);
if( is_pal(s1) ) fout << s << " "<< s1 <<endl;
}
return 0;
}
반응형