随机产生一个3位正整数,然后逆序输出,产生的随机数与逆序数在点击窗体时同时显示。

2025-03-12 12:41:23
推荐回答(2个)
回答1:

Private Sub Form_Click()
Randomize
Dim A As Integer
Dim B As Long
A = Int(Rnd * 900 + 100)
B = NiXuShu(A)
Print A; vbTab; B
End Sub
Private Function NiXuShu(ByVal l As String) As Long '逆序数计算
Dim i As Integer, j As Integer, c As Long
Dim n() As Integer
ReDim n(Len(l))
For i = 1 To Len(l)
n(i) = Val(Mid(l, i, 1))
For j = 1 To i - 1
If n(i) < n(j) Then
c = c + 1
End If
Next j
Next i
NiXuShu = c
End Function

回答2:

Randomize
Dim A As Integer
A = Int(Rnd * 900 + 100)

Print A, Left(A, 1), Mid(A, 2, 1), Right(A, 1)
Print Right(A, 1); Mid(A, 2, 1); Left(A, 1)