代码:
Private Sub Command1_Click()Dim Count As Integer, EverNumber As Integer
Dim strN As String, Temp As String
strN = Text1.Text
Label1.Caption = "您输入的数字是" + strN + vbCrLf
Count = 1
Label1.Caption = Label1.Caption + "第" & Count & "次:" + CStr(strN) + vbCrLf
Do
Temp = strN
Count = Count + 1
EverNumber = EverNumberCount(strN)
strN = CStr(EverNumber) + CStr(Len(strN) - EverNumber) + CStr(Len(strN))
If strN <> Temp Then Label1.Caption = Label1.Caption + "第" & Count & "次:" + strN + vbCrLf
Loop Until strN = Temp
Label1.Caption = Label1.Caption + "对于数字" + Text1.Text + ",经过" & Count - 1 & "次计算得到最终结果" & strN & ",算法收敛,结束。"
End Sub
Private Function EverNumberCount(ByVal s As String) As Integer
Dim EverNumber As Integer
For i = 1 To Len(s)
If Val(Mid(s, i, 1)) Mod 2 = 0 Then EverNumber = EverNumber + 1
Next i
EverNumberCount = EverNumber
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
Beep
End If
End Sub
设计界面:
运行界面:
黑洞运算任取一个数,例如35962,数出这数中的偶数个数、奇数个数及所有数字的个数,就可得到2(2个偶数)、3(3个奇数)、5(总共五位数),用这3个数组成下一个数字串235。对235重复上述程序,就会得到1、2、3,将数串123再重复进行,仍得123。对这个程序和数的“宇宙”来说,123就是一个数字黑洞。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)