重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
看了你说递归的效率低。那么你可以不用的。
创新互联公司专注于企业成都营销网站建设、网站重做改版、八步网站定制设计、自适应品牌网站建设、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类型的字典。