排列组合的算法:
举例:Cmn,m=2,n=3。
定义一个字符串,长度为n-1,初始化为0,将字符串的前面m位设置为1,代表被之前的数组对应位被选择上了。剩下的就是一个翻牌游戏,遇到第一个10,翻为01,直到所有的1被翻到了最右面就可以了。
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
代码:
‘函数调用方法 strData="" strData=FstrPermu(arr2,4)
'计算出排列组合的值,输出一个字符串 function FstrPermu(arr1(),m)
dim lngUB dim str1 dim arrP() dim lngI dim strRes dim lngLoop1
lngUB=ubound(arr1,1)+1 str1=String(lngUB,"0") str1=replace(str1,"0","1",1,m)
lngI=1 redim arrP(lngI) arrP(lngI-1)=str1
while right(str1,m)<>string(m,"1") lngI=lngI+1 str1=replace(str1,"10","01",1,1) redim preserve arrP(lngI) arrP(lngI-1)=str1 wend
strData="" For j=0 to UBound(arrP) strData=strData & arrP(j) & vbcrlf Next call Write2file(".\03.log",strData)
strRes=""
for lngLoop1=0 to ubound(arrP) for lngLoop2=0 to lngUB if mid(arrP(lngLoop1),lngLoop2+1,1)="1" then strRes=strRes & arr1(lngLoop2) & "," end if next strRes=left(strRes,len(strRes)-1) & vbcrlf next
FstrPermu=strRes end function |
需要排列组合算法的同学可以直接使用