Dim selected(10) As Boolean
Private Sub CommandButton1_Click()
h = 1
For i = 123 To 987
For j = 123 To 987
For k = 123 To 987
For c = 1 To 9
selected(c) = False
Next c
If k = i + j And check(i) And check(j) And check(k) Then
Cells(h, 1) = i
Cells(h, 2) = j
Cells(h, 3) = k
h = h + 1
End If
Next k
Next j
Next i
End Sub
Function check(a)
a1 = Int(a / 100)
a2 = Int((a Mod 100) / 10)
a3 = a - 100 * a1 - 10 * a2
If selected(a1) Then
check = False
Else
selected(a1) = True
If selected(a2) Then
check = False
Else
selected(a2) = True
If selected(a3) Then
check = False
Else
selected(a3) = True
check = True
End If
End If
End If
End Function
快速求解版
Dim selected(10) As Boolean
Private Sub CommandButton1_Click()
h = 1
For i = 123 To 987
For c = 0 To 9
selected(c) = False
Next c
If Not check(i) Then GoTo nnexti
For j = 123 To 987
For c = 0 To 9
selected(c) = False
Next c
If Not check(i) Or Not check(j) Then GoTo nnextj
For k = i+j To 987
For c = 0 To 9
selected(c) = False
Next c
If k = i + j And check(i) And check(j) And check(k) Then
Cells(h, 1) = i
Cells(h, 2) = j
Cells(h, 3) = k
h = h + 1
End If
Next k
nnextj:
Next j
nnexti:
Next i
End Sub
Function check(a)
a1 = Int(a / 100)
a2 = Int((a Mod 100) / 10)
a3 = a - 100 * a1 - 10 * a2
If selected(a1) Then
check = False
Else
selected(a1) = True
If selected(a2) Or a2 = 0 Then
check = False
Else
selected(a2) = True
If selected(a3) Or a3 = 0 Then
check = False
Else
selected(a3) = True
check = True
End If
End If
End If
End Function