andre_joy

导航

USACO palsquare

题意:找1~300对应的平方数再转换成对应的进制后是回文数字的数。

mark:扫描一遍,转换一遍,满足要求的输出。

代码:

/*
ID: andre_j2
LANG: C
TASK: palsquare
*/
#include <stdio.h>
#include <string.h>

int pd(char a[])
{
    int n,i,j;
    n = strlen(a);
    for(i = 0, j = n-1; i < j; i++, j--)
        if(a[i] != a[j]) return 0;
    return 1;
}

void fout(char a[])
{
    int i;
    for(i = strlen(a)-1; i >= 0; i--)
        printf("%c", a[i]);
}

main () {
    freopen("palsquare.in", "r", stdin);
    freopen("palsquare.out", "w", stdout);
    char s[25] = "0123456789ABCDEFGHIJ";
    char a[20],c[20];
    int b,n,m,i;
    scanf("%d", &b);
    for(n = 1; n <= 300; n++)
    {
        m = n*n;
        i = 0;
        while(m)
        {
            a[i++] = s[m%b];
            m /= b;
        }
        a[i] = '\0';
        if(pd(a))
        {
            i = 0;
            m = n;
            while(m)
            {
                c[i++] = s[m%b];
                m /= b;
            }
            c[i] = '\0';
            fout(c);
            printf(" ");
            fout(a);
            printf("\n");
        }
    }
    exit (0);
}

posted on 2012-07-09 22:30  andre_joy  阅读(89)  评论(0编辑  收藏  举报