[Today bored to this point, special post a test for a bit]
The implementation of common algorithm in VB
This is based on the information obtained from the Internet, because the usual algorithm is implemented with C programs, almost do not see the implementation of VB program, for unfamiliar with C language people will have many difficulties. So to do a VB to achieve a variety of algorithms, I do not know whether it is useful to everyone.
In order for the computer to complete the work that people have scheduled, it is necessary to write computer programs. The computer program should give a correct and detailed description of each object and processing rule of the problem, in which the data structure and variables of the program are used to describe the problem object, the program structure, function and the algorithm that the statement uses to describe the problem. Algorithms and data structures are two important aspects of a program. And the program is written according to the algorithm.
The algorithm is an exact description of the problem solving process, and an algorithm consists of the instructions with definite result which can be executed completely mechanically by the finite strip. The instructions correctly describe the tasks to be completed and the order in which they are executed. The computer executes the instruction of the algorithm according to the order described in the algorithm instruction, can terminate in a finite step, or terminate the solution to the problem, or terminate to indicate that there is no solution to the input data.
Usually solving a problem may have a variety of algorithms to choose from, the main criteria for selection is the correctness and reliability of the algorithm, simplicity and ease of understanding. The second is that the algorithm needs less storage space and executes faster.
Algorithm design is a very difficult work, but the design of the algorithm has a certain method, often used in the algorithm design techniques are mainly iterative method, exhaustive search method, recursive method, recursive method, greedy method, backtracking method, divide and conquer method, dynamic programming method and so on.
The following is a brief introduction to these algorithms, and the implementation of the program in VB.
First, iterative method
Iterative algorithm is a basic method to solve the problem by computer. It uses the computer speed, suitable for repetitive operation characteristics, so that the computer to a set of instructions (or a certain number of steps) repeated execution, each time the set of instructions (or these steps), the variable from the original value of the introduction of its new value.
Using an iterative algorithm to solve the problem requires doing the following three things:
First, determine the iteration variables. In a problem that can be solved with an iterative algorithm, there is at least one variable that, directly or indirectly, is continuously introduced with the new value by the old value, which is the iteration variable.
Second, establish an iterative relationship. The so-called iterative relationship, which refers to the formula (or relationship) that introduces its next value from the previous value of the variable. The establishment of iterative relationships is the key to solving iterative problems, which can usually be accomplished by means of recursive or backward-pushing.
Thirdly, the iterative process is controlled. When to end the iteration process. This is an issue that must be considered in writing an iterative procedure. You cannot allow iterative processes to repeat indefinitely. The control of an iterative process can usually be divided into two situations: one is that the desired number of iterations is a definite value that can be calculated, and the other is that the number of iterations required cannot be determined. In the former case, a fixed number of loops can be constructed to control the iterative process, and in the latter case, the conditions for ending the iterative process need to be further analyzed.
Example 1. Verify the valley angle conjecture. The Japanese mathematician, JINGFE, found a strange phenomenon when studying the natural numbers: for any natural number n, if n is even, divide it by 2, and if n is odd, multiply it by 3 and then add 1. The natural number 1 can always be obtained after the finite operation. The discovery of the valley JINGFE is called the "valley conjecture".
Now write a program, the keyboard input a natural number n, the n after a finite number of operations, and eventually become the natural numbers 1 of the whole process of printing out.
Analysis: Define the iteration variable to n, according to the content of Gucher conjecture, can get the iterative relation of two cases: when n is even, N=N/2; when n is odd, n=n*3+1. This is the iterative process that requires repeated computer execution. This iterative process needs to be repeated several times to make the iteration variable n eventually become the natural number 1, which we cannot calculate. Therefore, it is also necessary to further determine the conditions used to end the iterative process. Careful analysis of the requirements of the topic, it is not difficult to see, for any given a natural number N, as long as the finite number of operations, the natural numbers can be obtained 1, has completed the verification work. Therefore, the conditions used to end the iterative process can be defined as: N=1. The reference program is as follows:
Cls
n = InputBox ("Please enter a natural number n", "input")
Print N
Do Until n = 1
If N Mod 2 = 0 Then
n = N/2
Print "N/2 ="; N
Else
n = n * 3 + 1
Print "N * 3 + 1 ="; N
End If
Loop
Print "valley-angle conjecture to"; n; " Be confirmed. "
Example 2. Suppose that a pair of rabbits can have a pair of rabbits a month, and the rabbit will grow up after one months can start to have a rabbit, ask in a year a pair of big rabbits can breed how many pair of big rabbit.
Analysis: This is a typical recursive problem, but it can also be solved using iterative methods. We might as well assume that the 1th month when the logarithm of the rabbit is U1, the 2nd month when the logarithm of the rabbit is U2, 3rd months when the logarithm of the rabbit is U3, ... According to the test instructions, there are:
There were a pair of big rabbits, so u1=1. The second month, the big Rabbit gave birth to a pair of small rabbits, the rabbit did not grow up, the big rabbit is still a pair, u2=1. The third month, the small rabbit grew up, but did not have a small rabbit, the original Big Rabbit and a pair of rabbits, so the big rabbit is u3=u1+u2=2,....,n months later, the total number of large rabbits:
Un+1=un+un-1
By having the computer repeat 11 times for this iterative relationship, you can figure out the number of rabbits in the 12th month. The reference program is as follows:
x0 = 0 '
X1 = 1
For i = 1 to 12
x = x0 + x1
X1 = X0
x0 = x
Print "Big Rabbits a year later"; x; " Right. "
Next
Example 3. The amoeba breeds in a simple, split way, which takes 3 minutes each time it is split. A number of amoeba were placed in a container filled with nutrients, and after 45 minutes the container was filled with amoeba. A known container can be up to 220 amoeba. How many amoeba were placed in the container at the beginning. Please compile the program to figure out.
Analysis: According to test instructions, amoeba splits every 3 minutes, then from the beginning of the amoeba into the container, to 45 minutes after filling the container, need to split 45/3=15 times. According to test instructions, the number of amoeba splits after 15 times is 2^20. To calculate the number of amoeba before splitting, you can use the backward method, from the 2^20 after the 15th division, to roll out the number after the 14th Division, and then further reverse the 13th split, after the 12th Division 、...... The number of the number after the 1th division to the 1th time before division.
The number before the 1th division is x0, the number after the 1th division is X1, and the number after the 2nd division is X2 、...... The number of Xn after the nth division is
Xn=xn-1*2 or XN-1=XN/2
Because the number of splits is known, if the iteration variable is defined as x, then the iterative formula is repeated for a known number of times. Therefore, a fixed number of loops can be used to achieve the control of the iterative process. The reference program is as follows:
Cls
X=2^20
For I=1 to 15
X=x/2
Next
Print "At the beginning of the container is placed"; x; " An amoeba. "
Iterative method is also used to find the approximate root of the equation or the equations of a common algorithm. Set the equation to f (x) = 0, and derive its equivalent form x=g (x) by a mathematical transformation, which can then be performed as follows:
(1) Select an approximate root of the equation and assign it to the variable x0;
(2) The value of x0 is stored in the variable x1, then g (x1) is calculated, and the result is stored in the variable x0;
(3) When the absolute value of the difference between the x0 and the X1 is less than the specified precision, repeat step (2) calculation.
If the equation has roots, and the approximate root sequence converges by the above method, then the x0 obtained by the above method is considered to be the root of the equation.
The iterative method widely used in solving nonlinear equations in mathematics is Newton's iterative method, which is based on the equation F (x) Taylor expansion to take its linear part of the structure of the iterative formula, the general form is:
X=x-f (x)/F ' (x)
Example 4. Using Newton iterative method to find the root of the F (x) =ax3+bx2+cx+d=0 of the one-dimensional three-order equation.
F (x) =ax3+bx2+cx+d
g (x) =f ' (x) =3ax2+2bx+c
Its VB program is as follows:
x0 = 1
Do Until Abs (x-x0) <= y
x = x0
f = A * x ^ 3 + b * x ^ 2 + c * x + D
g = 3 * A * x ^ 2 + 2 * b * x + C
x0 = x-f/g
Loop
Print x0
The equations of the coefficients a, B, C, D, and the precision of the root y are known, or are entered by the user.
The following two possible scenarios should be noted when using iterative methods for root-finding:
(1) If the equation is not solved, the approximate root sequence obtained by the algorithm will not converge, the iterative process will become a dead loop, so before using the iterative algorithm, we should first investigate whether the equation has a solution, and in the program to limit the number of iterations;
(2) Although the equation has a solution, the iterative formula selection is inappropriate, or the initial approximate root selection of the iteration is unreasonable, which can lead to the failure of the iteration.
2006-5-4 13:56
#1