Usaco exercise: palindromic squares

Source: Internet
Author: User

The core of this question is the judgment of the retrieved numbers. There are basically two methods to judge the background.

1. converting to a string for determination is also a reference in the answer. (The idea is relatively simple. I will not discuss it here)

2. Use the MOD method to determine (only numbers can be judged) in mathematics. The advantage of this method is that you should not convert a number into a hexadecimal expression and then judge it.

The method is clever.

 

Next I will explain the method 2.

First, let's start with the simplest decimal language, for example, there are 123 numbers.

Decimal representation: 1*10 ^ 2 + 2*10 + 3

How can we turn this number upside down?

0*10 + 123% 10 = 3 then 123/10 = 12

3*10 + 12% 10 = 32 and then 12/10 = 1

32*10 + 1% 10 = 321

Do you see any rule?

 

Let's take a look at a certain number: 12321. Obviously, 12321 is 12321.

So we come to the conclusion that a number is the same as the original number when and only when the number is reversed.

 

In the preceding example, we use a decimal system as an example. Now let's think about the binary system.

How do we convert hexadecimal to binary?

12% 2 = 0, 12/2 = 6

6% 2 = 0, 6/2 = 3

3% 2 = 1, 3/2 = 1

1% 2 = 1

After 12 is converted, It is 1100. Have you found anything? In fact, the characters generated from 10 to 2 are exactly the reversed binary numbers of 12, that is

In the same way as the decimal system, you can get the value of decimal system upside down, and then compare it with the original value.

Some people may say: the original value is in decimal format. Isn't it different to convert it to binary instead?

In fact, this is a relatively round-robin. Whether it is in decimal or decimal format, their values are the same.

That is to say, the value reversed by binary is equal to the original 10-> 2-> reversed-> the value of this number is obtained using binary.

In other words, no matter how the hexadecimal conversion is, its value remains unchanged, that is, the values of hexadecimal and binaryValueThe comparison is reasonable.

 

Retrieval judgment code

BoolIs_palindrome (IntNum,Int Base){
IntReversed =0, Temp = num;
While(Temp! =0){
Reversed = reversed *Base+ TEMP %Base;
Temp/=Base;
}
Return(Reversed = num );
}

 

If you do not know about it, debug more.Code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.