Algorithm aabb, algorithm aabb

Source: Internet
Author: User

Algorithm aabb, algorithm aabb
Description: outputs the four-digit full records (that is, the first two digits are equal, and the last two digits are also equal) in the form of aabb ).When branch and loop are combined, the function is powerful:

The following lists all possible results of aabb, and then determines whether they are full records. Note that the range of a is 1 ~ 9, but B can be 0.

1 for (int a = 1; a <= 9; a ++) 2 for (int B = 0; B <= 9; B ++) 3 If (aabb is the total number of bytes) 4 printf ("% d \ n", aabb );

The above program is incomplete--"Aabb is a complete vertex number" is a Chinese description, rather than a legal c expression, and aabb is another variable in C language, rather than combining two numbers a and B. In this case, the "code" that is "not a real program" becomes a pseudo code ). Although there are some formal pseudo-code definitions, in actual application, it is not necessary to stick to the code format too much. The main goal is to describe the outline of the algorithm, avoid details, and inspire ideas.

After writing pseudo code, we need to consider how to turn it into real code. The above pseudo code has two "invalid" places; the determination of the full limit number, and the variable aabb. The latter is relatively easy. You can store it with another variable n = a × 1100 + B × 11.

The next question is more difficult: how can we determine whether n is a full shard number?

Method 1:PS (floor (x) is also written as Floor (x). Its function is to "round down" or "round down", that is, to take the maximum integer not greater than x)

1 # include <stdio. h> 2 # include <math. h> 3 int main () 4 {5 for (int a = 1; a <= 9; a ++) 6 for (int B = 0; B <= 9; B ++) 7 {8 int n = a * 1100 + B * 11; // n is used here, so n 9 int m = floor (sqrt (n) is defined here) + 0.5); 10 if (m * m = n) 11 printf ("% d \ n", n); 12} 13 return 0; 14}

Can it be written like this? If (sqrt (n) = floor (sqrt (n) printf ("% d \ n", n); that is, you can directly determine whether sqrt (n) is an integer. Theoretically, this is fine, but it is not safe to write, because the floating point calculation (and function) may have errors. Assume that after a large number of calculations, the integer 1 is changed to 0.99999999 due to the error, and the result of floor is 0 rather than 1 ,. In order to reduce the influence of the error, it is generally changed to rounding, that is, floor (x + 0.5 ). If it is hard to understand, you can imagine moving a unit interval to the left of 0.5 units on the number axis.

The range where Floor (x) is equal to 1 is [1, 2], and the range where floor (x + 0.5) is equal to 1 is [0.5, 1.5 ].

Floating point operations may have errors. When the floating point operation is compared, the floating point error should be taken into account.

Conclusion: A decimal part of 0.5 is also affected by the floating point error. Therefore, any rigorous algorithm competition must solve this problem.

Another idea is to enumerate the square root of x to avoid square-root operations.

# Include <stdio. h> int main () {for (int x = 1; x ++) // The for loop does not specify the loop condition. If you expect that the cycle starts from 32, you do not need to judge 1000 {int n = x * x;
If (n <1000) continue; if (n> 9999) break; int high = n/100; int low = n % 100; if (high/10 = high % 10 & low/10 = low % 10) printf ("% d \ n", n) ;}return 0 ;}

The answer is:

 

.

Related Article

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.