10.
能在excel里做个彩票缩水软件吗?
给m个数,选出n个数,一共有多少种排列,且全部列举出来.
Excel里能做的事很多,应该包括缩水软件。
至于给m个数,选出n个数,一共有多少种排列,可用下面这个函数实现:
在【工具】菜单中点击【宏】【Visual Basic编辑器】命令;
在打开的VB编辑器的左侧资源窗口中选中【Book1】或【personal】(如果你想你的电脑里的每个Excel文档都能用就用后一个)工程文档;
点击【插入】菜单中【模块】命令,插入一个模块;
双击该模块,在模块中输入下面的代码:
Function myCombin(strs As String, M As Integer, GoBack As Long, aaa As Boolean) As String
Dim sResult() As String
Dim sResultB() As String
Dim sArray() As String
Dim N As Integer
Dim nNum As Long
Dim nNumb As Long
Dim i As Long, j As Long
Dim tempV As Long
sArray = Split(strs, ",")
N = UBound(sArray) + 1
If M > N Then
myCombin = "#Value_M"
Exit Function
End If
nNum = N ^ M
nNumb = nNum
ReDim sResult(nNum - 1)
ReDim sResultB(nNum - 1)
Do While nNumb > 1
nNumb = nNumb / N
For i = 0 To nNum - 1
tempV = Int(i / nNumb) Mod N
sResult(i) = sResult(i) & sArray(tempV)
If InStr(sResultB(i), sArray(tempV)) <= 0 Then sResultB(i) = sResultB(i) & sArray(tempV)
Next
Loop
If aaa Then
If (GoBack <= nNum) And GoBack > 0 Then
myCombin = sResult(GoBack - 1)
Else
myCombin = "#Result"
End If
Else
tempV = 1
For i = 0 To nNum - 1
If Len(sResultB(i)) < M Then
If i >= nNum - tempV - 1 Then Exit For
For j = i To nNum - tempV - 1
sResultB(j) = sResultB(j + 1)
Next
i = i - 1
tempV = tempV + 1
End If
Next
tempV = nNum - tempV
ReDim Preserve sResultB(tempV)
If (GoBack <= tempV) And GoBack > 0 Then
myCombin = sResultB(GoBack - 1)
Else
myCombin = "#Result"
End If
End If
End Function
回到Excel文档中输入“=mycombin("a,b,c",3,1,1)”,就可知结果。
“mycombin()”函数的说明:
一、参数说明:
第一个参数:M 个供筛选的数,前后用英文字符的双引号括起来,每个数之间用英文的逗号隔开
第二个参数:每次从第一个参数中选出 N 个字符来排列组合
第三个参数:你要返回排列结果中的第几个组合
第四个参数:排列结果中是否允许重复数字,0表示不允许
实际应用:
A B
1 1 =mycombin("4,7,8",3,A1,1)
2 2 =mycombin("4,7,8",3,A2,1)
3 3 .
4 4 .
5 5 .
6 6 .
7 7 .
|