Analyze this question. 1. Generate K unequal integers. 2. sort and find the largest one. 3. display. Specifically, pay attention to the following issues: 1. Generate a random number, define an array, and generate each number in sequence. 2. to solve the non-repetition problem, you must first be bold. Assume that the first number produced by the array is the largest, and then compare it with each number in the following array to obtain the maximum number. During the comparison process, there will be two situations: one is that there are no duplicates, the number is saved, the comparison continues smoothly, the result is obtained, and the other is that there are duplicates, you have to discard this number, generate a new one, save the new non-repeated number, and continue. It is worth mentioning that there will be a benchmark here to tell you how to repeat and what to do without repeat. This is very important.
The Code is as follows:
Dim A () as long
Private sub form_click ()
Dim K as integer, M as integer, N as integer
K = Val (inputbox ("Enter K") 'generates K Integers
M = Val (inputbox ("Please input m") 'integer range, M is the lower bound
N = Val (inputbox ("Enter N") 'n' is the upper bound
Redim A (k-1)
Temp = int (m + (n-m + 1) * RND () 'randomly generates a number and assigns it to the first number of arrays A (0)
C = temp
A (0) = C
Print A (0)
'Number of possible duplicates after processing the second one
For I = 1 to k-1
C = int (m + (n-m + 1) * RND ())
Flag = 1' if you do not repeat the flag here, it is an important benchmark!
For T = 0 to I-1 'Here, the maximum number obtained previously is compared with the number of I generated so far.
If C = a (t) then
I = I-1 'discard this number and generate another number
Flag = 0
Exit
End if
Next t
If flag = 1 then', save it to the array.
A (I) = C
Print C' display on screen
End if
Next I
End sub