我想到一个办法,就是转成2进制不就行了吗!
比如你输入5,先得出5个1的二进制为31,(自己解决)
然后:
写一个将10进制转成二进制的函数,比如:
Public Function DecToBinary(dec As Integer)
Dim m As String, n As String, d As Integer
d = dec
Do
m = (d Mod 2) & m
d = d \ 2
Loop Until d = 0
DecToBinary = m
End Function
然后再:
dim i as Integer
dim s as string
for i=0 to 31
s=str(DecToBinary(i))
print s & vbcrlf
next i
另一种方法,不用转换为二进制,
Dim M, a() As Integer, i, P
M = 4: ReDim a(M - 1)
Do
P = M - 1
For i = 0 To P
Debug.Print a(i);
Next: Debug.Print
Do
a(P) = a(P) + 1
If a(P) > 1 Then a(P) = 0: P = P - 1 Else Exit Do
Loop While P > -1
Loop While P > -1
Private Sub CreateArray(ByVal M As Integer, ByRef ABuf)
ReDim ABuf(2 ^ M - 1, M - 1)
Dim iRow As Integer
Dim iCol As Integer
Dim iNum As Integer
For iRow = 0 To 2 ^ M - 1
iNum = iRow
For iCol = M - 1 To 0 Step -1
ABuf(iRow, iCol) = iNum Mod 2
iNum = iNum \ 2
Next iCol
Next iRow
End Sub
Private Sub Command1_Click()
Dim A() As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = InputBox("输入M的值")
CreateArray k, A
For i = 0 To 2 ^ k - 1
For j = 0 To k - 1
Debug.Print A(i, j);
Next j
Debug.Print
Next i
End Sub