algorithm practice to achieve the goal
1. Do 1 form , including two text boxes
2. Add 25 random numbers to the Access text box
3. Require numbers not to be repeated
4. Sorting html from small to large
4. ml1 final effect
main application technology:
1.ForNext prevent duplication
2. Bubble algorithm sort
practice steps
Step 1: Make the output end (form)
make two text boxes and two button controls. One text box is used to show the arrangement of non-repetitions, and the other text box is used to show the arrangement of repetitions.
Step 2: Generate 25 random numbers (1-30), and require not to repeat
code as follows
For i = 1 To 25
a(i) = Int(30 * Rnd + 1)
For j = 1 To i - 1 'This paragraph is the code to prevent duplication
If a(i) = a(j) Then 'If repeated select
i = i - 1
End If
Next j
Next i
Step 2 The technical point is to create an array a and use a ForNext to assign array values.
's difficulty is to deduplicate 25 random numbers. The method is to introduce a new variable j, and compare it with the previous number for each generated, and re-select it if the same. The ForNext operation is also used.
deduplication operation code
For j = 1 To i - 1 'This paragraph is the code to prevent duplication
If a(i) = a(j) Then 'If repeated select again
i = i - 1
End If
Next j
Step 3: Sort with the bubble algorithm
The core idea of the bubble algorithm is to introduce an intermediate variable t and constantly compare adjacent numbers. The core idea of the bubble algorithm is to press large numbers down and decimal numbers forward. It's like a bubble walking forward. I will write another article to introduce the bubble algorithm, and this article will only give a brief introduction.
code is as follows:
For i = 1 To 25 - 1
For j = i + 1 To 25
If a(i) a(j) Then
t = a(i) 't As an intermediate variable, bubble algorithm is common
a(i) = a(j)
a(j) = t
End If
Next j
Next i
25 digit generation and numerical sorting are set in one sub. You can operate
by clicking a button once, Step 4: Break the input segment
For the convenience of demonstration, 25 digits need to be branched. Return every time you count to 5.
tempStr is an intermediate variable, and its purpose is to separate numbers and perform a carriage return operation at multiples of 5. Use If function to distinguish.
Chr(13) and Chr(10) are the carriage return symbols of the computer.
For i = 1 To 25
Text1 = Text1 + tempStr + CStr(a(i)) 'CStr converted to string
If i = 5 Or i = 10 Or i = 15 Or i = 20 Or i = 25 Then
tempStr = Chr(13) + Chr(10) + " " 'Line-breaking
Else
tempStr = " "
End If
Next i
complete code
Option Compare Database
Dim i As Integer
Dim j As Integer
Dim tempStr As String 'Intermediate variable, used for sequence branch
Dim a(1 To 25) As Double 'Array has 25 numbers in total
Dim t As Double 'Intermediate variable, sorting is done with
Private Sub Command1_Click()
Text1 = ""
tempStr = " "
'The following paragraph generates 25 numbers that do not repeat. Select
For i = 1 To 25
a(i) = Int(30 * Rnd + 1)
For j = 1 To i - 1 'This paragraph is a code to prevent duplication
If a(i) = a(j) Then 'If it is repeated, select
i = i - 1
End If
Next j
Next i
'This paragraph is sorting, using the bubble algorithm
For i = 1 To 25 - 1
For j = i + 1 To 25
If a(i) a(j) Then
t = a(i) 't As an intermediate variable, bubble algorithm is common
a(i) = a(j)
a(j) = t
End If
Next j
Next i
'This paragraph generates 25 numbers in the text box
For i = 1 To 25
Text1 = Text1 + tempStr + CStr(a(i)) 'CStr converted to string
If i = 5 Or i = 10 Or i = 15 Or i = 20 Or i = 25 Then
tempStr = Chr(13) + Chr(10) + " " 'Line-breaking
Else
tempStr = " "
End If
Next i
End Sub
Private Sub Command2_Click()
Text2 = ""
tempStr = " "
'The following paragraph is the 25 numbers generated (may be repeated). Select
For i = 1 To 25
a(i) = Int(30 * Rnd + 1)
Next i
'This paragraph is sorted, using the bubble algorithm
For i = 1 To 25 - 1
For j = i + 1 To 25
If a(i) a(j) Then
t = a(i)
a(i) = a(j)
a(j) = t
End If
Next j
Next i
'This paragraph generates 25 numbers in the text box
For i = 1 To 25
Text2 = Text2 + tempStr + CStr(a(i))
If i = 5 Or i = 10 Or i = 15 Or i = 20 Or i = 25 Then
tempStr = Chr(13) + Chr(10) + " "
Else
tempStr = " "
End If
Next i
End Sub
.