看了你說遞歸的效率低。那么你可以不用的。
創新互聯公司專注于企業成都營銷網站建設、網站重做改版、八步網站定制設計、自適應品牌網站建設、HTML5、商城網站制作、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為八步等各大城市提供網站開發制作服務。
給出的方法就是先生成第一個排列,然后每次調用下面的函數給出下一個排列,這樣生成的效率很高,這個函數可以內聯。
這個是很經典的排列組合算法啊?在網上能搜到一大堆。
大概是那種帶指向的移動的算法。我給你搜一個吧。
我找了幾個,這個是我覺得說的比較清楚的,你可以仔細參考一下,看不懂的話再搜點別的好了。。
全排列的算法跟這個不太一樣的。需要有點改動的。
至于語言的話,應該不會有太大問題吧。。basic版的確實比較少,現在我也比較懶不想動手寫。。還是要靠你自己啦。
★生成排列的算法:
比如要生成5,4,3,2,1的全排列,首先找出一個最小的排列12345, 然后依次調用n!次STL算法中的next_permutation()即可輸出所有的全排列情況。所以這種算法的細節就是STL algorithm中next_permutation()的實現機制。詳細的實現代碼,大伙可以參考侯捷的《STL源代碼剖析》,在這里我只說一下我的理解:
1 首先從最尾端開始往前尋找兩個相鄰元素,令第一個元素為*i,第二個元素為*ii,且滿足*i*ii,找到這樣一組相鄰的元素后。
2 再從最尾端開始往前檢驗,找出第一個大于*i的元素,令為*k,將i,k元素對調。
3 再將ii及ii之后的所有元素顛倒排列,此即所求之"下一個"排列。
prev_permutation()算法的思路也基本相同,只不過它們尋找的"拐點"不同,在next_permutation()算法中尋找的是峰值拐點,而在prev_permutation()算法中尋找的是谷值拐點。另外,在第二步中,prev_permutation()要找的是第一個小于*i的元素而不是第一個大于*i的元素。
具體例子,有空再舉,現在時間太晚了:)
★生成組合的算法:
如下面截圖所示,分全組合和r-組合兩種情況。
這里有一段核心代碼:
//--------------------------------------------------------
// Generate next combination (algorithm from Rosen p. 286)
//--------------------------------------------------------
public int[] getNext () {
if (numLeft.equals (total)) {
numLeft = numLeft.subtract (BigInteger.ONE);
return a;
}
int i = r - 1;
while (a[i] == n - r + i) {
i--;
}
a[i] = a[i] + 1;
for (int j = i + 1; j r; j++) {
a[j] = a[i] + j - i;
}
numLeft = numLeft.subtract (BigInteger.ONE);
return a; //這里返回的a數組,存儲的就是下標的排列組合。
}
到這里,也許大伙會有一個疑問,假如要求的不是數字的排列組合,而是字符或字符串的排列組合呢?怎么辦?其實很簡單,你只要拿數組的下標來做排列組合,返回他們下標的排列組合,然后再到原數組中讀取字符串值,就可以輸出全部的排列組合結果。
Dim result As New Dictionary(Of Byte, Byte())() From { _
1, _
New Byte() {Hff, H0, H0} _
}
Public Class Form1
Inherits System.Windows.Forms.Form
Public filename As String = "英漢詞典.txt"
Public myword(6500, 1) As String
Public words As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String
Dim b As Integer
Dim i As Integer = 0
Dim n As String
Dim m As String
Dim stringb As Integer
TextBox1.Text = ""
TextBox2.Text = ""
FileOpen(1, "英漢詞典.txt", OpenMode.Input)
Do While Not EOF(1)
a = LineInput(1)
b = InStr(a, " ")
n = Microsoft.VisualBasic.Left(a, b - 1)
myword(i, 0) = n
ListBox1.Items.Add(n)
stringb = Len(a) - b
m = Trim(Microsoft.VisualBasic.Right(a, stringb))
myword(i, 1) = m
i += 1
Loop
words = i
FileClose(1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = -1
If TextBox1.Text = "" Then
MessageBox.Show("不能輸入空格,請重新輸入")
TextBox2.Text = ""
TextBox1.Focus()
Exit Sub
Else
For i = i + 1 To words
If LCase(TextBox1.Text) = LCase(myword(i, 0)) Then
TextBox2.Text = Trim(myword(i, 1))
Exit Sub
End If
Next
MessageBox.Show(" 您需要的單詞不存在,請重新輸入")
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Try
TextBox1.Text = myword(ListBox1.SelectedIndex, 0)
TextBox2.Text = Trim(myword(ListBox1.SelectedIndex, 1))
Catch ex As Exception
End Try
Exit Sub
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ch, enterwords As String
Dim j, m As Integer
If -1 = ListBox1.SelectedIndex Then
MsgBox("請選擇單詞", , "")
ListBox1.Focus()
Exit Sub
End If
enterwords = InputBox("請修改單詞", "修改單詞", Trim(myword(ListBox1.SelectedIndex, 0)))
Do While enterwords = ""
m = MsgBox("單詞不能為空", MsgBoxStyle.RetryCancel, "修改單詞")
If m = 4 Then
enterwords = InputBox("請修改單詞", "修改單詞", Trim(myword(ListBox1.SelectedIndex, 0)))
Else
Exit Sub
End If
Loop
ch = InputBox("請修改中文意思", "修改單詞", Trim(myword(ListBox1.SelectedIndex, 1)))
Do While ch = ""
m = MsgBox("中文意思不能為空", MsgBoxStyle.RetryCancel, "修改單詞")
If m = 4 Then
ch = InputBox("請修改中文意思", "修改單詞", Trim(myword(ListBox1.SelectedIndex, 1)))
Else
Exit Sub
End If
Loop
myword(ListBox1.SelectedIndex, 1) = ch
myword(ListBox1.SelectedIndex, 0) = enterwords
FileOpen(1, filename, OpenMode.Output)
For j = 0 To words - 1
PrintLine(1, myword(j, 0) " " myword(j, 1))
Next
FileClose(1)
MsgBox("修改成功")
ListBox1.Items.Clear()
Form1_Load(sender, e)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer = 0
Dim k, m As Integer
Dim enterwords, ch As String
enterwords = InputBox("請輸入要添加的單詞", "添加單詞")
Do While enterwords = ""
m = MsgBox("單詞不能為空,請輸入單詞!", MessageBoxButtons.RetryCancel, "添加單詞")
If m = 4 Then
enterwords = InputBox("請輸入要添加的單詞", "添加單詞")
Else
Exit Sub
End If
Loop
ch = InputBox("請輸入中文意思", "添加中文")
Do While ch = ""
m = MsgBox("中文不能為空,請輸入中文意思!", MessageBoxButtons.RetryCancel, "添加中文")
If m = 4 Then
ch = InputBox("請輸入中文意思", "添加中文")
Else
Exit Sub
End If
Loop
Do While LCase(myword(i, 0)) LCase(enterwords)
i = i + 1
If words = i Then
myword(i, 0) = enterwords
myword(i, 1) = ch
words = words + 1
FileOpen(1, filename, OpenMode.Output)
For i = 0 To words - 1
PrintLine(1, myword(i, 0) " " myword(i, 1))
Next
ListBox1.Items.Clear()
FileClose(1)
ListBox1.Items.Clear()
Form1_Load(sender, e)
MessageBox.Show("添加成功")
Exit Sub
End If
Loop
If LCase(myword(i, 0)) = LCase(enterwords) Then
MessageBox.Show("該單詞已存在!")
ListBox1.SelectedIndex = i
Exit Sub
ElseIf LCase(myword(0, 0)) LCase(enterwords) Then
For k = words To 0 Step -1
myword(k + 1, 0) = myword(k, 0)
myword(k + 1, 1) = myword(k, 1)
Next
myword(0, 0) = enterwords
myword(0, 1) = ch
words = words + 1
FileOpen(1, filename, OpenMode.Output)
For i = 0 To words - 1
PrintLine(1, myword(i, 0) " " myword(i, 1))
Next
ListBox1.Items.Clear()
FileClose(1)
Form1_Load(sender, e)
MessageBox.Show("添加成功")
Exit Sub
End If
For k = words To i + 1 Step -1
myword(k + 1, 0) = myword(k, 0)
myword(k + 1, 1) = myword(k, 1)
Next k
myword(i, 0) = enterwords
myword(i, 1) = ch
words = words + 1
FileOpen(1, filename, OpenMode.Output)
For i = 0 To words - 1
PrintLine(1, myword(i, 0) " " myword(i, 1))
Next
FileClose(1)
ListBox1.Items.Clear()
Form1_Load(sender, e)
MessageBox.Show("添加成功")
Exit Sub
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim i, j, k As Integer
If -1 = ListBox1.SelectedIndex Then
MsgBox("請選擇單詞", , "")
ListBox1.Focus()
Exit Sub
End If
k = MsgBox("確定是否刪除", MsgBoxStyle.YesNo, "提示")
If k = 6 Then
For i = ListBox1.SelectedIndex To words
myword(i, 0) = myword(i + 1, 0)
myword(i, 1) = myword(i + 1, 1)
Next
words = words - 1
FileOpen(1, filename, OpenMode.Output)
For j = 0 To words - 1
PrintLine(1, myword(j, 0) " " myword(j, 1))
Next
FileClose(1)
MsgBox("單詞已刪除")
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
ListBox1.Refresh()
TextBox1.Text = ""
TextBox2.Text = ""
Exit Sub
Else
Exit Sub
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class
這是代碼,文字性的內容自己去做。
泛型參數,表示一種特定類型,通常用于集合List, Dictionary之類的。
原型:Dictionary(Of TKey, TValue)
原型不能直接使用,必須給TKey, TValue指定一個類型(Type)
強類型字典:Dictionary(Of String, String)
表示鍵和值都為String類型的字典。
文章名稱:關于vb.net字典使用的信息
網站網址:http://vcdvsql.cn/article36/doisdpg.html
成都網站建設公司_創新互聯,為您提供靜態網站、網站排名、企業網站制作、面包屑導航、動態網站、品牌網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯