MATLAB practice | Find the number of daffodils

The basic idea of ​​the loop structure is repetition, that is, taking advantage of the computer's fast computing speed and ability to perform logical control, certain statements are repeatedly executed to meet a large number of calculation requirements. Although the statements executed in each loop are the same, the values ​​of some variables in the statements change, and the loop can end when the loop reaches a certain number of times or the conditions are met. Loops are an important feature of computer problem-solving and an important technique in programming. MATLAB provides two statements to implement loop structures: the for statement and the while statement.

01, circulation structure

1. for statement

In general, for loop structures where the number of loops can be determined in advance, it is more convenient to use the for statement. The format of the for statement is as follows:

 

Where, "expression1:expression2:expression3" is a colon expression that will produce a row vector. The value of expression 1 is the initial value of the loop variable, the value of expression 2 is the step size, and the value of expression 3 is the final value of the loop variable. When the step size is 1, expression 2 can be omitted.

The execution process of the for statement is shown in Figure 1. First, calculate the values ​​of 3 expressions to generate a row vector, and then assign the elements in the vector to the loop variable one by one. After each assignment, the loop body statement is executed. When all elements of the vector have been used, the for statement ends. execution, while continuing to execute the statements following the for statement.

 

Regarding the execution process of the for statement, the following points should be explained.

(1) The for statement executes the loop body once for each element of the vector. The number of loops is the number of elements in the vector, and it can also target any vector. For example, the following loop structure loops 4 times in total, and the values ​​of k are -1, 32, 20, and 5 respectively.

picture

(2) The three expressions in the for statement are only evaluated once at the beginning of the loop, that is, the vector elements will not change once they are determined. If there are variables in the expression, the elements of the vector will not change even if the value of the variable is changed in the loop body. For example, the vector elements in the following for statement are 1, 3, 5, 7, and 9. The elements of the vector will not be changed by changing the value of n in the loop body.

picture

(3) After exiting the loop, the value of the loop variable is the value of the last element in the vector. For example, the vector elements in the following for statement are 1, 3, 5, 7, 9, and the k value after the for loop is 9.

picture

(4) When the vector is empty, the loop body is not executed once. For example, the colon expression in the following for statement produces an empty vector, that is, there are no elements in the vector, and the loop is not executed once.

picture

[Example 1]If the sum of the cubes of the digits of a 3-digit integer is equal to the number itself, the number is called the narcissus number. Output the total number of daffodils.

Use the exhaustive method to judge all 3-digit integers one by one, and then find out all the daffodil numbers. To determine the narcissus number, the key step is to first find the ones, tens, and hundreds digits of a three-digit integer, and then determine whether the number is a narcissus number based on the conditions. The procedure is as follows:

 

The results of running the program are as follows:

Guess you like

Origin blog.csdn.net/qq_41640218/article/details/134756025