填空题
以下程序的功能是验证"数学黑洞6174"。对于任意一个各位数字不全部相同的四位整数,将组成该数的数字组合成一个最大的数和一个最小的数,二者相减,判断其差值是否为6174,若不是,再将相减的结果组合成新的最大的数和最小的数,以上过程最多重复7次,必得结果6174。程序运行界面如图所示。
Option Explicit
Option Base 1
Private Sub Cmd1_Click1
Dim i As Integer,k As Integer
Dim max As Integer min As Integer
k=Val(Text1)
For i=1 T0 3 '判断4位数字是否全部一样
If Mid(CStr(k),i,1)<>Mid(CStr(k),i+1,1) Then 2(26) 3
Next i
If i>3 Then
MsgBox "输入的数据不合要求"
Text1=""
Exit Sub
End If
For i=1 To 7
Call Validate(k max,min)
List1.AddItcm k &":" & max &"-" & min & "=" & max-min
If max-min=6174 Then
Exit For
Else
k= 4(27) 5
End If
Next i
If 6(28) 7 Then List1.AddItem "验证失败!"
End Sub
Private Sub Validate(n As Integer,max As Integer,min As Integer)
Dim i As Integer,t(4)As Integer
Dim s As String,s1 As String,s2 As String
S=CStr(n)
For i=1 To 4
t(i)=Val(Mid(s,i,1))
Next i
Call sort(t)
For i=1 To 4
s1=s1 & CStr(t(i))
s2= 8(29) 9
Next i
min=Val(s1)
max=VaI(s2)
End Sub
Private Sub sort(a10 As Integer)
Dim i As Integer,j As Integer,t As Integer
For i=1 To UBound(a)-1
For j=i+1 To UBound(a)
If 11(30) 12 Then
t=a(i):a(i)=a(j):a(j)=t
End If
Next j
Next i
End Sub