Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Assume that the linear table adopts a sequential storage structure, and the element values ??in the table are integers. Read Algorithm f 30 and answer the following questions
Assume that the linear table adopts a sequential storage structure, and the element values ??in the table are integers. Read Algorithm f 30 and answer the following questions

You can first think about how to start if a person does this.

The answer is very simple,

1. Get a number,

2. Check whether this number is repeated with the number you have already seen. If it is repeated...otherwise...

Obviously, each of the two steps requires a cycle.

It’s similar: there are several white balls marked with numbers in the sequence. Look at these white balls in order. If there is a new number on the ball, paint the ball red. Otherwise, if it is a repeated number, paint the ball red. Become green. By the way, every time you check a new number, do you need to compare it with all the balls you have already seen? No need, just compare it with the red ball.

The program also performs a shift coverage operation: when an unrepeated ball is found, if there is a green ball in the sequence, the first green ball in the sequence is replaced with the current ball and marked. red. If there is no green ball in the sequence, the current ball will be painted red.

This ensures that the sequence will always maintain this order:

A number of red balls, a number of green balls, and a number of white balls.

Next, let’s clarify the meaning of each variable:

i is the variable used to traverse the white ball. Since it is only traversed once, it can be interpreted as the position of the first white ball in the sequence.

k is the first position after all red balls. If k=i, it means there is no green ball for the time being. Otherwise k points to the position of the first green ball in the sequence.

j is a variable used to traverse the red ball. The range of the red ball is [0,k), so the value of j is [0,k]. If j=k, it means there is no repetition.

Do you understand this? Look at the code again. Please write your own comments.