The AABB of the algorithm

Source: Internet
Author: User
Tags square root

Tag: enumeration operation break maximum result. com Floor Understanding Continue

Title Description : output all the 4 -bit full squares (i.e., the first two digits equal to the AABB The latter two digits are also equal). The features are powerful when branching and looping together:

Below is a list of all possible results aabb, and then determine if they are full squares. 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 full squared)  4                        printf ("%d\n", AABB); 

The program above is not complete. --"AABB is a complete square number" is a Chinese description, not a valid C language expression, and AABB is another variable in C, instead of putting two numbers a and b together. This makes "code" such as "not a real program" a pseudo-code (pseudocode). Although there are some formal pseudo-code definitions, in practical applications, it is not necessary to be too rigidly tied to the format of the code. The main goal is to describe the algorithm outline, avoid the details, and inspire ideas.

after writing the pseudo-code, we need to consider how to turn it into real code. The pseudo code above has two "illegal" places, a complete square number determination, and a AABB variable. The latter is relatively easy; n=ax1100+bx11 storage with another variable.

The next question is a bit more difficult: how can we tell if N is a complete squared number?

method One: PS (Floor (x), also write floor (x), its function is "rounding down", or "rounded down", that is, the largest integer not greater than x)

1#include <stdio.h>2#include <math.h>3 intMain ()4 {  5     for(intA=1; a<=9; a++)  6        for(intb=0; b<=9; b++)  7       {  8          intn=a*1100-bl One;//here we start using N, so here we define N9int M=floor (sqrt (n) +0.5);Ten          if(m*m==N) Oneprintf"%d\n", N);  A       }   -    return 0;  -}

Could you write that? if (sqrt (n) ==floor (sqrt (n))) printf ("%d\n", N), which directly determines whether sqrt (n) is an integer. Theoretically, of course, no problem, but this is not insurance, because the operation of floating-point numbers (and functions) there may be errors. Assuming that after a lot of calculations, because of the error, the integer 1 becomes the result of the 0.99999999,floor will be 0 instead of 1. In order to reduce the effect of the error, it is generally changed to rounding, i.e. floor (x+0.5). If it is difficult to understand, you can imagine on the axis of a unit interval to the left 0.5 units distance.

Floor (x) is equal to 1 of the interval is "x+0.5", and Floor (a) is equal to 1 of the interval is "0.5,1.5".

There may be errors in floating-point arithmetic. The floating-point error should be taken into account when comparing floating-point operations.

Summary: The number of decimal parts divided into 0.5 will also be affected by floating-point errors, so any rigorous algorithm contest topics need to find a way to solve this problem

Another idea is to enumerate the square root x, thus avoiding the radical operation.

#include <stdio.h>intMain () { for(intx=1;; X + +)//The For loop does not specify a loop condition if you expect to start from 32, you don't have to judge .   {        intn=x*X
    if (n<1000) continue; if(N>9999) Break; inthigh=n/ -; intlow=n% -; if(high/Ten==high%Ten&&low/Ten==low%Ten) printf ("%d\n", N); } return 0; }

The answer is

.

The AABB of the algorithm

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.