One Day One Step 之Palindromic Squares

这一道题目比较简单,我说一下题意!输入一个数表示进制。要求求出1~300以内的某一个数,这个数的平方在该进制下是回文数。输出这个数以及这个数的平方(10进制 )!


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 60
#define LIMIT 300

void transtostr( int num, int base, char * numtostring )
{
    int i = 0;
    int TmpBit;

    while( num > 0 )
    {
        TmpBit = num % base;
        if( base > 10 && TmpBit >= 10 )
        {
            *( numtostring + i ) = TmpBit - 10 + 'A';
        }
        else
        {
            *( numtostring + i ) = TmpBit + '0';
        }
        num /= base;
        i++;
    }
}

int isPalindromic( char * str, int len )
{
    int i;
    int isP = -1;

    for( i = 0; i <= len / 2; i++ )
    {
        if( *( str + i ) != *( str + len - 1 - i ) )
        {
            isP = 0;
            break;
        }
    }

    return isP == 0 ? -1 : 1;
}

int main()
{
    FILE * fin = fopen( "palsquare.in", "r" );
    FILE * fout = fopen( "palsquare.out", "w" );
    int base;
    int len;
    int lenofnum;
    char num[N] ={0};
    char numtostring[N] = {0};

    fscanf( fin, "%d", &base );
    //scanf( "%d", &base );
    int i, j;

    for( i = 1; i <= LIMIT; i++ )
    {
        transtostr( i * i, base, numtostring );
        len = strlen( numtostring );
        if( isPalindromic( numtostring, len ) == 1 )
        {
            transtostr( i, base, num );
            lenofnum = strlen( num );
            for( j = lenofnum - 1; j >= 0; j-- )
            {
                //printf( "%c", num[j] );
                fprintf( fout, "%c", num[j] );
            }
            //printf( " " );
            //printf( "%s\n", numtostring );
            fprintf( fout, " " );
            fprintf( fout, "%s\n", numtostring );
        }
            //printf( "%s\n", numtostring );
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u011564456/article/details/20410211
one