我正在尝试解决特定的切割库存问题。这一切都归结为解决以下任务,至少在我的解决方案中: 我想查找长度为n的向量的所有排列,其中包含m个。,例如:
在这种情况下(11100):n = 5且m = 3
溶液:
11100 11010 11001 10110 10101 10011 01110 01101 01011 00111
我知道如何计算可能性的数量,但不知道如何以智能和有效的方式获得实际的向量。
我正在与Vb.Net合作,在编程方面经验不足。是否有针对该问题的.Net解决方案?如果不是,我会感谢您帮助开发自定义解决方案。
谢谢。
答案 0 :(得分:0)
我为你编写了一个能够做到这一点的功能。
以下是示例输出:
以下是代码:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
For Each s As String In getPermutations(5, 3).ToArray
sb.AppendLine(s)
Next
Clipboard.SetText(sb.ToString)
MsgBox(sb.ToString)
End Sub
Public Iterator Function getPermutations(m As Integer, n As Integer) As IEnumerable(Of String)
Dim highest As Integer = CInt(2 ^ m)
For I As Integer = 0 To highest - 1
Dim s As String = Convert.ToString(I, 2).PadLeft(m, "0"c)
If oneCount(s) = n Then
Yield s
End If
Next
End Function
Function oneCount(ref As String) As Integer
Dim result As Integer = 0
For Each ch As Char In ref
If ch = "1" Then result += 1
Next
Return result
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
getPermutations(3, 1)
End Sub
End Class