一个排列组合问题
现有一字符串数组: sp = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}如何实现数字“0”到“1”转变的所有排列组合?
举例:
sp中含1个“1”的排列组合有:
sp = {"1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
sp = {"0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
sp = {"0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0"}
sp = {"0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0"}
sp = {"0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0"}
sp = {"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0"}
sp = {"0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0"}
sp = {"0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0"}
sp = {"0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0"}
sp = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "0"}
sp = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0"}
sp = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1"}
sp中含2个“1”的排列组合
。。。
sp中含12个“1”的排列组合
sp = {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"}
求从sp中含1个“1”一直到含12个“1”的排列组合算法 --------------------编程问答-------------------- ' n为数量
' 这个算前边的还好,算后边数量变少的时候就慢,你把算法倒过来算12附近的会快些!
Dim sp As String = "0 0 0 0 0 0 0 0 0 0 0 0"
Dim n As Byte = 3
Dim Re(50000) As String
Dim x As Integer = -1
Dim s As Integer = 0
Dim num As Byte = 1
Private Sub xxx(ByVal c As Integer)
ListBox1.Items.Add(" ")
ListBox1.Items.Add("开始输出" & num & "个1集合")
Dim ssp() As String
Dim i, j As Integer
Dim text As String = ""
For j = s To c
ssp = Split(Re(j), " ")
For i = 0 To 11
text = ""
If ssp(i) = 0 Then
x += 1
ssp(i) = 1
text = Join(ssp, " ")
Re(x) = text
ListBox1.Items.Add(text)
ssp(i) = 0
End If
Next
Next
s = c + 1
num += 1
If num > N Then Exit Sub
xxx(x)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Re(0) = "0 0 0 0 0 0 0 0 0 0 0 0"
xxx(0)
End Sub --------------------编程问答-------------------- 不好意思,我这个生成的很多重复的..... --------------------编程问答-------------------- 自己顶一个,好久没来了,怎么现在人气这么差,以前要不了半天就有好多热心的朋友帮忙
--------------------编程问答-------------------- 是不是太复杂了?有没人给点思路,望高手指点 --------------------编程问答-------------------- 条件过于巧合,如果基于这个巧合
那么直接+1就可以了 --------------------编程问答-------------------- 你把我写的那个过滤一下就可以了! --------------------编程问答--------------------
' n为数量--------------------编程问答-------------------- 考试题。自己是新手,等待高手解答
' 这个算前边的还好,算后边数量变少的时候就慢,你把算法倒过来算12附近的会快些!
Dim sp As String = "0 0 0 0 0 0 0 0 0 0 0 0"
Dim n As Byte = 3
Dim Re(50000) As String
Dim x As Integer = -1
Dim s As Integer = 0
Dim num As Byte = 1
Private Sub xxx(ByVal c As Integer)
ListBox1.Items.Add(" ")
ListBox1.Items.Add("开始输出" & num & "个1集合")
Dim ssp() As String
Dim i, j As Integer
Dim text As String = ""
For j = s To c
ssp = Split(Re(j), " ")
For i = 0 To 11
text = ""
If ssp(i) = 0 Then
x += 1
ssp(i) = 1
text = Join(ssp, " ")
Re(x) = text
ListBox1.Items.Add(text)
ssp(i) = 0
End If
Next
Next
s = c + 1
num += 1
If num > N Then Exit Sub
xxx(x)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Re(0) = "0 0 0 0 0 0 0 0 0 0 0 0"
xxx(0)
End Sub
补充:.NET技术 , VB.NET