ACwing 1346 palindrome square

Title description

The number of palindrome refers to the number that reads from front to back and from back to front is the same number.

For example, the number 1232112321 is a typical palindrome number.

Now given you an integer BB, please determine which of all the integers between 1∼3001∼300, after the squares of the integers are converted into BB base, the BB base represents a palindrome number.

Input format

An integer BB.

Output format

Each line contains two numbers in BB base .

The first one represents the number that satisfies the squared value and is converted to the BB base, and the second number represents the square of the first number.

All numbers that meet the conditions are output in order from smallest to largest.

data range

2≤B≤202≤B≤20,
for numbers greater than 99, use AA for 1010, BB for 1111, and so on.

Input sample:

10

Sample output:

1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696
#include <iostream>
#include <cstdio>

using namespace std;
const int MAX = 309;

int B, k;
char a[MAX];
char b[MAX];

bool judge(int x)
{
    k = 0;
    while(x)
    {
        if(x % B > 9)
            a[k ++] = 'A' + (x % B - 10);
        else
            a[k ++] = x % B + '0';
        x /= B;
    }

    for(int i = 0; i < k / 2; i++)
    {
        if(a[i] != a[k - i - 1])
            return false;
    }
    return true;
}

int main()
{
    scanf("%d", &B);

    for(int i = 1; i <= 300; i++)
    {
        if(judge(i * i))
        {
            int j = i;
            int kk = 0;
            while(j)
            {
                if(j % B > 9)
                    b[kk++] = 'A' + (j % B - 10);
                else
                    b[kk++] = j % B + '0';

                j /= B;
            }

            for(int p = kk - 1; p >= 0; p--)
                printf("%c", b[p]);
            printf(" ");

            for(int p = 0; p < k; p++)
                printf("%c", a[p]);
            printf("\n");
        }

    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/112990712