| |

VerySource

 Forgot password?
 Register
Search
View: 1023|Reply: 9

Permutation of 0,1.

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-26 00:00:01
| Show all posts |Read mode
There are M 0,1 permutations, which are printed out by the program. If there are 3 0,1 permutations, you should type:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
How to generate this, please help masters, thanks in advance! By the way, the value of M is not known in advance and can be changed temporarily.
Reply

Use magic Report

0

Threads

21

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-2-24 12:30:01
| Show all posts
I think of a way to do it is to convert it to binary.
For example, if you enter 5, you will first get 5 1's binary as 31, (solve it yourself)
then:
Write a function that converts decimal to binary, for example:
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

after that:
dim i as Integer
dim s as string
for i = 0 to 31
  s = str (DecToBinary (i))
  print s&vbcrlf
next i
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-10 06:15:01
| Show all posts
It seems that I did not make it clear that I intend to store these permutations in an array, and I will use these numbers at that time.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-10 14:30:01
| Show all posts
That is to get a column number:
a (0) = 0 a (1) = 0 a (2) = 0
a (0) = 0 a (1) = 0 a (2) = 1
         .
         .
         .
a (0) = 1 a (1) = 1 a (2) = 1
Hope to get such a result.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-14 19:30:02
| Show all posts
I wrote a function CreateArray for you, and have an example:

Option Explicit

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 ("The value of input 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
Reply

Use magic Report

0

Threads

34

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-23 15:45:01
| Show all posts
Another method, without converting to binary,
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
Reply

Use magic Report

0

Threads

34

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-23 20:30:01
| Show all posts
It should be faster than converting to binary
Where the array a () stores each value
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-29 11:15:01
| Show all posts
Auto-increment from 0, converted to binary each time and stored in the array
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-3 09:30:01
| Show all posts
If it were me, I would have the following methods
1: Database Text files can also be operated as databases
2: Add data to LISTVIEW or list
Sorted property

Returns a value that specifies whether the elements of the control are automatically sorted alphabetically.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-11 00:15:01
| Show all posts
Thank you for your help!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list