作业帮 > 综合 > 作业

VB语言从11选6全组合相连

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/17 15:56:54
VB语言从11选6全组合相连
从1,2,3.11这11个数字中任取6个全组合输出(每组有不同的6个数字组成)
并且满足条件每组有2个相连号(比如1-2,2-3等叫做相连号)
高手来啊要有源代码
下面输出结果就正确
01-02-04-06-08-10
01-02-04-06-08-11
01-02-04-06-09-11
01-02-04-07-09-11
01-02-05-07-09-11
01-03-04-06-08-10
01-03-04-06-08-11
01-03-04-06-09-11
01-03-04-07-09-11
01-03-05-06-08-10
01-03-05-06-08-11
01-03-05-06-09-11
01-03-05-07-08-10
01-03-05-07-08-11
01-03-05-07-09-10
01-03-05-07-10-11
01-03-05-08-09-11
01-03-05-08-10-11
01-03-06-07-09-11
01-03-06-08-09-11
01-03-06-08-10-11
01-04-05-07-09-11
01-04-06-07-09-11
01-04-06-08-09-11
01-04-06-08-10-11
02-03-05-07-09-11
02-04-05-07-09-11
02-04-06-07-09-11
02-04-06-08-09-11
02-04-06-08-10-11
每组有2个相连号,难道还不清楚明白还要在文字上钻
VB语言从11选6全组合相连
01-02-04-06-08-10里有2个相连号?
按我的理解写的代码
Dim s(0 To 6) As String
Private Sub Form_Load()
s1 = Split("1,2,3,4,5,6,7,8,9,10,11", ",")
add s, s1, 1, 6, 1
End Sub
Sub add(s, s1, n1, n2, n3)
For i = n1 To n2
s(n3) = s1(i - 1)
If n3 < 6 Then
add s, s1, i + 1, n2 + 1, n3 + 1
Else
s2 = s(1)
For i2 = 2 To 6
s2 = s2 & "-" & s(i2)
Next
lianhao = 0
For i3 = 1 To 10
If InStr(s2, i3 & "-" & i3 + 1) > 0 Then
lianhao = lianhao + 1
End If
Next
If lianhao = 2 Then
s3 = s3 & s2 & vbCrLf
End If
End If
Next
Text1.Text = Text1.Text & s3
End Sub
再问: 编译不成功啊
再答: 加了文本框了吗?记得设置为多行哦。
再问: 如何设置多行啊 点解第一句有这个Dim s(0 To 6) As String
再答: 将文本框的MultiLine属性设置为TRUE就是多行了。 定义数组。
再问: 好似也不行 不如你把它输出到一个txt文本中保存更好,但不知道如何实现
再答: Dim s(0 To 6) As String, s3 As String Private Sub Form_Load() s1 = Split("1,2,3,4,5,6,7,8,9,10,11", ",") add s, s1, 1, 6, 1 Open App.Path & "\1.txt" For Output As #1 Print #1, s3 Close #1 '运行记事本打开输出结果文本 Shell "notepad " & """" & App.Path & "\1.txt" & """", vbNormalFocus End Sub Sub add(s, s1, n1, n2, n3) For i = n1 To n2 s(n3) = s1(i - 1) If n3 < 6 Then add s, s1, i + 1, n2 + 1, n3 + 1 Else s2 = s(1) For i2 = 2 To 6 s2 = s2 & "-" & s(i2) Next lianhao = 0 For i3 = 1 To 10 If InStr(s2, i3 & "-" & i3 + 1) > 0 Then lianhao = lianhao + 1 End If Next If lianhao = 2 Then s3 = s3 & s2 & vbCrLf End If End If Next End Sub
再问: 这个结果好似不正确每组只能有2个号码相连即1-2, 但输出结果1-2-3-5-7-9,就有1-2-3共3个号码相连了
再答: If lianhao = 2 Then s3 = s3 & s2 & vbCrLf End If 这里改为 If lianhao = 1 Then s3 = s3 & s2 & vbCrLf End If