Usaco:Palindromic Squares

简单的进制转换

/*
ID: Jang Lawrence
PROG: palsquare
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char a[]={"0123456789ABCDEFGHIJKLMN"};
void dfs(int x)
{
    if(x>=n) dfs(x/n);
    putchar(a[x%n]);
}
bool is(int x)
{
    char t[30];
     int i=0;
     while(x)
     {
         t[i++]=a[x%n];
         x/=n;
     }
     for(int j=0;j<i;++j)
     if(t[j]!=t[i-1-j]) return false;
     return true;
}
int main()
{
    freopen("palsquare.in","r",stdin);
  freopen("palsquare.out","w",stdout);
   scanf("%d",&n);
   for(int i=1;i<=300;++i)
   if(is(i*i))
   {
       dfs(i);
       putchar(' ');
       dfs(i*i);
       puts("");
   }
    return 0;
}


你可能感兴趣的:(Usaco:Palindromic Squares)