摆个擂
看到wp版的“大家晒一晒自己的代码啊”和小z版的“两位数乘三位数等于四位数并且这九个数都不重复”就是“123456789”9个数,取2个组成一个两位数,取3个组成一个三位数,这两个数的积就是剩下的那四位数。
例12×483=5796
找出这些组合,用VB6实现。
[ 本帖最后由 Artless 于 2013-4-15 20:13 编辑 ]
Option Explicit Private Sub Command1_Click() Dim i As Long, j As Long, m As Long '二个循环变量及结果 Dim o As Long '判断用的循环变量 Dim k(1 To 9) As Long '数字是否已用 '49 - 57 Dim s1 As String '连接结果用变量 Dim k2() As Byte, b1 As Boolean '结果的BYTE数组和测试结果变量 Cls Print "开始时间:" & Time Debug.Print "开始时间:" & Time For i = 12 To 98 '不重复的二位数,从 12 开始到 98 For j = 123 To 987 '不重复的三位数,从 123 开始到 987 m = i * j If m < 10000 Then '结果要求为 4位数,也就是小于10000 s1 = i & j & m k2 = StrConv(s1, vbFromUnicode) '转化为 BYTE 数组 For o = 1 To 9 '计数数组初始化 k(o) = 0 Next o b1 = True '初始为真 For o = 0 To 8 If k2(o) = 48 Then '如果含零,则直接否决 b1 = False Exit For End If k(k2(o) - 48) = k(k2(o) - 48) + 1 '个数累计 If k(k2(o) - 48) > 1 Then '发现个数超过1,否决 b1 = False Exit For End If Next o If b1 Then Print i & " * " & j & " = " & m '输出结果 Debug.Print i & " * " & j & " = " & m End If Else Exit For End If Next j Next i Print "结束时间:" & Time Debug.Print "结束时间:" & Time End Sub
For o = 0 To 8 k2(o) = k2(o) - 48 'val If k2(o) = 0 Then '如果含零,则直接否决 b1 = False Exit For End If k(k2(o)) = k(k2(o)) + 1 '个数累计 If k(k2(o)) > 1 Then '发现个数超过1,否决 b1 = False Exit For End If Next o
For o = 1 To 9 '计数数组初始化 k(o) = False Next o b1 = True '初始为真 For o = 0 To 8 k2(o) = k2(o) - 48 'val If k2(o) = 0 Then '如果含零,则直接否决 b1 = False Exit For End If If k(k2(o)) Then '发现个数已用过,否决 b1 = False Exit For Else k(k2(o)) = True '标记为用过 End If Next o