Public Class Form1
Dim intNum(), intCode() As Integer '全局变量数组
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'初始化
'数值过大耗时较多,也可能出现溢出无法得到全部结果
txtM.Text = "15"
txtN.Text = "5"
End Sub
Private Sub btnCode_Click(sender As Object, e As EventArgs) Handles btnCode.Click
'对于用户输入值必须进行合理性检测,否则会出现异常错误、耗时过长等问题
If IsNumeric(txtM.Text) And IsNumeric(txtN.Text) Then
Try
Dim intM, intN As Integer
intM = txtM.Text
intN = txtN.Text
If intM > 0 And intN > 0 Then
If intM < 40 Then
If intN < intM Then
'现在开始进行正式的初始化和运算
ReDim intNum(intM - 1), intCode(intN - 1)
Dim intI As Integer
'将所有数字保存到数组中
For intI = 0 To intM - 1
intNum(intI) = intI + 1
Next
'初始化号码
For intI = 0 To intN - 1
intCode(intI) = 0
Next
'清空列表数据
lstCode.Items.Clear()
'试探法算法
combine(intM, intN)
'显示号码总数量
Me.Text = "号码总数量:" & lstCode.Items.Count
Else
MsgBox("N值不能大于或等于M值,请重新输入")
End If
Else
MsgBox("数字过大,耗时较多")
End If
Else
MsgBox("错误,不能是负数,请重新输入")
End If
Catch ex As Exception
MsgBox("出现异常错误,可能是数字超过正常范围,请输入40以内正整数")
End Try
Else
MsgBox("错误,不是数字,请重新输入")
End If
End Sub
Private Sub combine(ByVal intM As Integer, ByVal intN As Integer)
'试探法算法
'算法简单,但不太好理解。没关系,保存起来,用时粘贴一下
For intI As Integer = intM To intN Step -1
intCode(intN - 1) = intNum(intI - 1) '保存一个位置的数字
If intN > 1 Then
'递归,生成各个位置的数字
combine(intI - 1, intN - 1)
Else
'输出号码
Dim strCode As String = String.Empty
For intJ As Integer = intCode.Length - 1 To 0 Step -1
strCode = strCode & intCode(intJ) & " "
Next
lstCode.Items.Add(strCode)
End If
Next
End Sub
Private Sub 作品集ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 作品集ToolStripMenuItem.Click
Process.Start("https://pan.baidu.com/s/1jr_3Yt5l1i6jNNbMy4R61A")
My.Computer.Clipboard.Clear()
My.Computer.Clipboard.SetText("5ycz")
MsgBox("提取码5ycz已经复制,直接到网页中粘贴即可")
End Sub
Private Sub 代码浏览ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 代码浏览ToolStripMenuItem.Click
Process.Start("https://blog.csdn.net/kguncn/article/details/109261834")
End Sub
Private Sub 更新下载ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 更新下载ToolStripMenuItem.Click
Process.Start("https://pan.baidu.com/s/1oZif1xez9ivpLwJijYDMWA")
My.Computer.Clipboard.Clear()
My.Computer.Clipboard.SetText("jsyk")
MsgBox("提取码jsyk已经复制,直接到网页中粘贴即可")
End Sub
End Class
结合已编译程序看计算号码更好理解,程序可以通过代码中“更新下载”的链接和提取码进行下载。