不要随便叫难嘛。
其实编程真的不难,关键是要沉住气,静下心,全身心投入!
在重复字符判断上有点算法错误,之前意识到,没改到那里,稍作修改后不再出现了。
程序代码:
Private Function Straight(ByVal Sour As String) As String
'判断是否为顺子
Dim b() As String, i As Integer, j As Integer, a As String, f As Boolean
Straight = ""
b = Split(Sour, ",")
For i = 0 To 5
'对数据进行冒泡法排序
For j = i + 1 To 6
If Val(b(i)) > Val(b(j)) Then a = b(i): b(i) = b(j): b(j) = a
Next
Next
For i = 0 To 2
f = True
For j = Val(b(i)) + 1 To Val(b(i)) + 4
If InStr("," & Sour, "," & j & ",") = 0 Then
f = False
Exit For
End If
Next
If f Then
a = ""
For j = 0 To 6
a = a & b(j) & ","
Next
Straight = a
Exit Function
End If
Next
End Function
Private Sub PermComb(ByVal Sour As String, Result As String)
'递归排列组合所有
Dim b() As String, a As String, i As Integer
b = Split(Sour, ",")
If UBound(b) = 7 Then
a = Straight(Sour)
If a <> "" And InStr(Result, a) = 0 Then Result = Result & a & "|" '输出有顺子的不重复的结果
Exit Sub
End If
For i = 1 To 13
If InStr("," & Sour, "," & i & ",") = 0 Then PermComb Sour & i & ",", Result
Next
End Sub
Private Sub Command1_Click()
Dim a As String, b() As String, c() As String, d As String, i As Integer, j As Integer, s As Integer
Randomize
s = Int(Rnd * 3) + 5 '随机数据个数
a = ""
j = Int(Rnd * 13) + 1
For i = 1 To s
While InStr(a, j) > 0
j = Int(Rnd * 13) + 1
Wend
a = a & j & ","
Next
'上述代码产生随机数据在a里
'a = "10,2,5,3,9," '该数据实验用
'a = "6,3,11,12,4,"
's = 5 '该数据模拟实验用
Text1 = "原数据:" & Left(a, Len(a) - 1)
d = ""
PermComb a, d
If d <> "" Then
Text1 = Text1 & ",可以组成顺子。"
If s < 7 Then
b = Split(d, "|")
For i = 0 To UBound(b) - 1
c = Split(b(i), ",")
d = ""
For j = 0 To 6
If InStr("," & a, "," & c(j) & ",") = 0 Then d = d & c(j) & "," '这里同前面一样,加个逗号把数字分开即可
Next
Text1 = Text1 & d & "|"
Next
End If
Else
Text1 = Text1 & ",无法组成顺子。"
End If
End Sub