学习字符串 vb net 的排列 [英] learn permutation of string vb net
问题描述
我得到了一个字符数组,想用特殊条件创建一个排列
i got an array of char and want to create a permutation with special condition
例如,如果我输入 2,那么它将生成 2 个字符的排列char 数组包含 6 个像这样的元素 inChars = {c,f,a,b,m,p)
for example if i input 2, then it will generate permutation of 2 character
the char array contains 6 elements like this inChars = {c,f,a,b,m,p)
当我用
3个字符输入,它生成,f,a,b
3 character input, it generates, permutation of f,a,b
4 个字符 --> f,a,b,m
我的第一个问题是为什么它从数组 (f) 的第二个元素而不是 (c) 开始
my 1st question is why it begin from 2nd element of array (f) instead of (c)
和我的第二个问题如何创建这样的东西:当我输入 3 个字符时,它将生成 2 个和 3 个字符子集,而不是仅生成 3 个子集,就像
and my 2nd questions how to create something like this: when i input 3 character, it will generate 2 and 3 subset of char instead of only 3 subset, it will be like
c,f
c-a
....
c,f,a
c,a,m
.....
这是截图
这是代码
Public Class permute
Dim ItemUsed() As Boolean
Dim pno As Long, pString As String
Dim inChars() As Char = {"c", "f", "a", "b", "m", "p"}
Private Sub permute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Sub Permute(ByVal K As Long)
ReDim ItemUsed(K)
pno = 0
Permutate(K, 1)
tb.Text = K
End Sub
Private Sub Permutate(ByVal K As Long, ByVal pLevel As Long)
Dim i As Long, Perm As String
Perm = pString
For i = 1 To K
If Not ItemUsed(i) Then
If pLevel = 1 Then
pString = inChars(i)
Else
pString += inChars(i)
End If
If pLevel = K Then
pno = pno + 1
Results.Text += _
pno & " " & " = " & " " & pString & vbCrLf
Exit Sub
End If
ItemUsed(i) = True
Permutate(K, pLevel + 1)
ItemUsed(i) = False
pString = Perm
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Permute(tb.Text)
End Sub
Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb.TextChanged
If tb.Text = "" Then
Results.Text = ""
Else
Permute(tb.Text)
End If
End Sub
End Class
推荐答案
改变你的 For
循环,从 0
开始到 K-1
:
Change your For
loop to start from 0
to K-1
:
For i = 0 To K - 1
更新 - 第二个问题
Sub Permute(ByVal K As Long)
results = New List(Of String)
ReDim ItemUsed(K)
pno = 0
Dim i As Integer
For i = 2 To K
Permutate(i, 1)
Next
End Sub
这篇关于学习字符串 vb net 的排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!