紧急跪求一算法,分不够可加!!
0到9十个数中,我想得到一些组合数,这些组合数的规则是总共分为三个组,A,B,C 组,其中A和B组里面是0到九之间的任意三个数,但不可重复,剩下的四个数放到C组中,然后我想得到就是这种规则的组合共同多少种,并列举出来。
比如:
A:012 B:965 C:3478
以上只是其中的一种,就是这种格式!
在此谢过,重谢!!
[解决办法]
Private Sub Command1_Click()
lst.Clear
For i = 12 To 987
If IsRightNumber(i) = True Then
For j = 12 To 987
If IsRightNumber(j) = True And HaveSameNumber(i, j) = False Then
lst.AddItem ( "A= " & i & "; B= " & j)
End If
Next
End If
DoEvents
Next
End Sub
Private Function IsRightNumber(ByVal num As Integer) As Boolean
Dim sTmp As String
sTmp = num
Do While Len(sTmp) < 3
sTmp = "0 " + sTmp
Loop
Dim A, B, c
A = Left(sTmp, 1)
B = Mid(sTmp, 2, 1)
c = Right(sTmp, 1)
If A = B Or A = c Or B = c Then
IsRightNumber = False
Exit Function
End If
IsRightNumber = True
End Function
Private Function HaveSameNumber(ByVal num1 As Integer, ByVal num2 As Integer)
Dim A As String
Dim B As String
A = Right( "000 " & num1, 3)
B = Right( "000 " & num2, 3)
For i = 1 To 3
If InStr(A, Mid(B, i, 1)) > 0 Then
HaveSameNumber = True
Exit Function
End If
Next
HaveSameNumber = False
End Function
顯示太多了﹐只給出前兩個數的算法﹐第三個數很簡單﹐你自已取剩下的4個數的不同排列組合就可以了
[解决办法]
Option Explicit
'0到9十个数中,我想得到一些组合数,这些组合数的规则是总共分为三个组,A,B,C 组,
'其中A和B组里面是0到九之间的任意三个数,但不可重复,剩下的四个数放到C组中,
'然后我想得到就是这种规则的组合共同多少种,并列举出来。
'比如:
' A:012 B:965 C:3478
Private Sub Form_Load()
TextShow
End Sub
Private Sub TextShow()
'A从0-9中选3个数字,不能重复。全部打印
Dim i1 As Integer
Dim j1 As Integer
Dim k1 As Integer
Dim i2 As Integer
Dim j2 As Integer
Dim k2 As Integer
Dim i3 As Integer
Dim A(2) As String
Dim B(2) As String
Dim C As String
For i1 = 0 To 9
DoEvents
A(0) = CStr(i1)
For j1 = 0 To 9
DoEvents
If j1 <> CInt(A(0)) Then
A(1) = CStr(j1)
For k1 = 0 To 9
DoEvents
If k1 <> CInt(A(0)) And k1 <> CInt(A(1)) Then
A(2) = CStr(k1)
'Text1.Text = Text1.Text + A(0) & A(1) & A(2) & Chr(13) & Chr(10)
For i2 = 0 To 9
DoEvents
If i2 <> CInt(A(0)) And i2 <> CInt(A(1)) And i2 <> CInt(A(2)) Then
B(0) = CStr(i2)
For j2 = 0 To 9
DoEvents
If j2 <> CInt(A(0)) And j2 <> CInt(A(1)) And j2 <> CInt(A(2)) And j2 <> CInt(B(0)) Then
B(1) = CStr(j2)
For k2 = 0 To 9
DoEvents
If k2 <> CInt(A(0)) And k2 <> CInt(A(1)) And k2 <> CInt(A(2)) And k2 <> CInt(B(0)) And k2 <> CInt(B(1)) Then
B(2) = CInt(k2)
For i3 = 0 To 9
DoEvents
If i3 <> CInt(A(0)) And i3 <> CInt(A(1)) And i3 <> CInt(A(2)) And i3 <> CInt(B(0)) And i3 <> CInt(B(1)) And i3 <> CInt(B(2)) Then
C = C & i3
End If
Next
Text1.Text = Text1.Text + "A: " + A(0) & A(1) & A(2) + " B: " + B(0) & B(1) & B(2) + " C: " + C + Chr(13) & Chr(10)
C = " "
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
Next
End Sub