问题如下:
在“999”中添一个数字,使形成的四位数可以被四位数“1357”整除。你知道这个数字是几吗?好,告诉你,它是“4”,即“9499”。验证:9499÷1357=7 在“888”中添上两个数字,形成的五位数可以被“1357”整除,有趣的是得数就是你所填写的那两个数构成的两位数。 好了,把它做成VB题吧! 解题如下:
Dim Sum As Integer
Function B(x As Long) As Boolean
Dim Str As String, a As Integer, k As Integer
For i = 1 To Len(CStr(x))
y = Mid(x, i, 1)
If y = "8" Then
a = a + 1
Else
Str = Str & y
End If
Next
If a = 3 Then
For j = 1 To Len(CStr(Sum))
z = InStr(Str, Mid(Sum, j, 1))
If z <> 0 Then k = k + 1
Next
End If
If k = 2 Then
B = True
Else
B = False
End If a = 0
k = 0
Str = ""
End FunctionPrivate Sub Form_Click()
Dim Yswq As Long, WuWeiShu As Long, C As Boolean
Yswq = 1357
Sum = 1
Do While C <> True
Sum = Sum + 1
WuWeiShu = Yswq * Sum
C = B(WuWeiShu)
Loop
Print "这个两个数分别是:" & Mid(Sum, 1, 1) & "和" & Mid(Sum, 2, 1) & ",而这个五位数是:" & WuWeiShu & "!"
End Sub本人写代码的功力粗鄙,求优化代码!
在“999”中添一个数字,使形成的四位数可以被四位数“1357”整除。你知道这个数字是几吗?好,告诉你,它是“4”,即“9499”。验证:9499÷1357=7 在“888”中添上两个数字,形成的五位数可以被“1357”整除,有趣的是得数就是你所填写的那两个数构成的两位数。 好了,把它做成VB题吧! 解题如下:
Dim Sum As Integer
Function B(x As Long) As Boolean
Dim Str As String, a As Integer, k As Integer
For i = 1 To Len(CStr(x))
y = Mid(x, i, 1)
If y = "8" Then
a = a + 1
Else
Str = Str & y
End If
Next
If a = 3 Then
For j = 1 To Len(CStr(Sum))
z = InStr(Str, Mid(Sum, j, 1))
If z <> 0 Then k = k + 1
Next
End If
If k = 2 Then
B = True
Else
B = False
End If a = 0
k = 0
Str = ""
End FunctionPrivate Sub Form_Click()
Dim Yswq As Long, WuWeiShu As Long, C As Boolean
Yswq = 1357
Sum = 1
Do While C <> True
Sum = Sum + 1
WuWeiShu = Yswq * Sum
C = B(WuWeiShu)
Loop
Print "这个两个数分别是:" & Mid(Sum, 1, 1) & "和" & Mid(Sum, 2, 1) & ",而这个五位数是:" & WuWeiShu & "!"
End Sub本人写代码的功力粗鄙,求优化代码!
Private Sub Form_Load()
For i = 8 To 73 '
T = CStr(i * a)
k = 0
For j = 1 To 5
S = Mid(T, j, 1)
If S = "8" Then k = k + 1
Next
If k = 3 Then jg = T
Next
Debug.Print jg
For j = 1 To 5
S = Mid(jg, j, 1)
If S <> "8" Then
Debug.Print S
End If
Next
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer, k As Integer, L As Integer
Dim jg As String, s As String
Dim b(1) As String
Me.Show
For i = 8 To 73 '
T = CStr(CLng(i) * a)
k = 0
For j = 1 To 5
s = Mid(T, j, 1)
If s = "8" Then k = k + 1
Next
If k = 3 Then jg = T
Next
Debug.Print jg
For j = 1 To 5
s = Mid(jg, j, 1)
If s <> "8" Then
b(L) = s
L = L + 1
End If
Next
Print "这个两个数分别是:" & b(0) & "和" & b(1) & ",而这个五位数是:" & jg & "!"
End Sub
Private Sub Command1_Click()
Dim m_Cnt_i As Long
Dim m_MCnt_i As Long
Dim Yswq As Long
Dim m_flg_i As Long
Dim AA As Long
Yswq = 1357Dim Count As IntegerCUOUNT = 0
For m_MCnt_i = 1 To 100
AA = Yswq * m_MCnt_i
Count = 0
For m_Cnt_i = 1 To 3
m_flg_i = InStr(CStr(AA), 8)
If m_flg_i <> 0 Then
If m_flg_i = Len(CStr(AA)) Then
If m_Cnt_i = 3 Then
GoTo A
End If
Exit For
End If
AA = CLng(Trim(Mid(CStr(AA), m_flg_i + 1, Len(CStr(AA)))))
End If
If Count = 3 Then
GoTo A
End If
Count = Count + 1
Next
NextA:
MsgBox ("zhe个五位数是:" & Yswq * m_MCnt_i)
End Sub
'888添加两位时最大的五位数99888
'888添加两位时最小的五位数10888
Private Sub Form_Load()
Dim a, b, c, d, e, f, x
a = Int(10888 / 1357) * 1357
b = Int(99888 / 1357) * 1357
For c = a To b Step 1357
For d = 0 To 4
If InStr(d + 1, c, "8") Then
d = InStr(d + 1, c, "8")
e = e + 1
End If
Next
If e = 3 Then
Debug.Print c,
For f = 1 To 5
If Mid(c, f, 1) <> "8" Then _
Debug.Print "位数"; f, "数值"; Mid(c, f, 1),
Next
End If
e = 0
Next
Debug.Print x
End Sub
'888添加两位时最大的五位数99888
'888添加两位时最小的五位数10888
'x为总循环次数,用于测试程序算法的速度(暂无以时间判断速度的办法)。
Private Sub Form_Load()
Dim a, b, c, d, e, f, x
a = Int(10888 / 1357) * 1357
b = Int(99888 / 1357) * 1357
For c = a To b Step 1357
For d = 0 To 4
x = x + 1 '可删除,用于测试
If InStr(d + 1, c, "8") Then
d = InStr(d + 1, c, "8")
e = e + 1
End If
Next
If e = 3 Then
Debug.Print c,
For f = 1 To 5
x = x + 1 '可删除,用于测试
If Mid(c, f, 1) <> "8" Then _
Debug.Print "位数"; CStr(f), "数值"; Mid(c, f, 1),
Next
End If
e = 0
Next
Debug.Print "总循环次数"; x
End Sub
'以下是以字符串方式处理的,效率谈不上,权且当一种思路。
Private Sub Command1_Click()
Dim a, b, c, i, j, k, m, n, x
a = 888
For i = 0 To 3
For j = 0 To 9
b = Mid(a, 1, i) & j & Right(a, 3 - i)
For k = i + 1 To 4
For m = 0 To 9
x = x + 1
c = Mid(b, 1, k) & m & Right(b, 4 - k)
If c Mod 1357 = 0 Then
Debug.Print c,
For n = 1 To 5
x = x + 1
If Mid(c, n, 1) <> 8 Then
Debug.Print "位置"; CStr(n), "数值"; Mid(c, n, 1),
End If
Next
End If
Next
Next
Next
Next
Debug.Print "总循环次数"; x
Option ExplicitFunction B(x As Long) As Boolean
Dim s As String
Dim a As Integer
Dim i As Integer
s = CStr(x)
For i = 1 To Len(s)
a = a - (Mid(s, i, 1) = "8")
Next If a = 3 Then
s = Replace(s, "8", vbNullString)
End If
B = (Len(s) = 2)
End FunctionPrivate Sub Form_Click() Dim Yswq As Long
Dim WuWeiShu As Long
Dim sum As Long
Yswq = 1357
sum = 10888 \ Yswq
Do
WuWeiShu = Yswq * sum
sum = sum + 1
Loop Until B(WuWeiShu)
sum = sum - 1
Print "这个两个数分别是:" & Mid(sum, 1, 1) & "和" & Mid(sum, 2, 1) & ",而这个五位数是:" & WuWeiShu & "!"
End Sub
Dim s As String
Dim a As Integer
Dim i As Integer
s = CStr(x)
For i = 1 To Len(s)
a = a - (Mid(s, i, 1) = "8")
Next
B = (a = 3)
End FunctionPrivate Sub Form_Click() Dim Yswq As Long
Dim WuWeiShu As Long
Dim sum As Long
Yswq = 1357
sum = 10888 \ Yswq
Do
WuWeiShu = Yswq * sum
sum = sum + 1
Loop Until B(WuWeiShu)
sum = sum - 1
Print "这个两个数分别是:" & Mid(sum, 1, 1) & "和" & Mid(sum, 2, 1) & ",而这个五位数是:" & WuWeiShu & "!"
End Sub
Dim WuWeiShu As Long
Dim sum As Long
Yswq = 1357
sum = 10888 \ Yswq
Do
WuWeiShu = Yswq * sum
sum = sum + 1
Loop Until UBound(Split(CStr(WuWeiShu), "8")) = 3
sum = sum - 1
Print "这个两个数分别是:" & Mid(sum, 1, 1) & "和" & Mid(sum, 2, 1) & ",而这个五位数是:" & WuWeiShu & "!"
End Sub