USACO Palindromic Squares

  看一下n2的base进制是不是回文串, 是的话输出来即可, 直接暴力枚举:

/*
    ID: m1500293
    LANG: C++
    PROG: palsquare
*/

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int B;

int a[100], len;

bool check(int n)
{
    len = 0;
    int tp = n*n;
    while(tp > 0)
    {
        a[len++] = tp%B;
        tp /= B;
    }
    bool flog = true;
    for(int i=0; i<len/2; i++)
    {
        if(a[i] != a[len-1-i])
        {
            flog = false;
            break;
        }
    }
    return flog;
}

int b[100], len_b;

void trans(int n)
{
    len_b = 0;
    while(n > 0)
    {
        b[len_b++] = n%B;
        n /= B;
    }
    for(int i=len_b-1; i>=0; i--)
        if(b[i] < 10) printf("%d", b[i]);
        else printf("%c", 'A'+b[i]-10);
}

int main()
{
    freopen("palsquare.in", "r", stdin);
    freopen("palsquare.out", "w", stdout);
    while(scanf("%d", &B) == 1)
    {
        for(int n=1; n<=300; n++)
        {
            if(check(n)) 
            {
                trans(n);
                printf(" ");
                for(int i=0; i<len; i++)
                {
                    if(a[i] < 10) printf("%d", a[i]);
                    else printf("%c", 'A'+a[i]-10);
                }
                printf("\n");
            }
        }
    }
    return 0;
}

 

posted @ 2015-12-17 22:02  xing-xing  阅读(138)  评论(0编辑  收藏  举报