2010年10月18日 星期一

0 與 1 的遊戲

內容 : 
有 1 個 bit,可以表示 0 與 1。
有 2 個 bit,可以表示 00,01,10,11。
有 n 個 bit,請產生所有 n 個 bit 所能表示的 2 進位數字。
輸入說明 :
每行一個數字 n ( 0 < n < 15 )
代表 n 個bit
輸出說明 :
 請參考範例輸出
範例輸入 :
2
範例輸出 :
00 
01 
10 
11
提示 :
字串是你的好朋友

6 則留言:

  1. Dim aa(1000000) As String
    Dim stri As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, x
    aa(0) = String(x, "0"): aa(1) = String(x - 1, "0") & 1
    m = 2: n = 2
    For i = 1 To x - 1
    stri = String(x - m, "0") & "1"
    k = n - 1
    For j = 0 To k
    aa(n) = stri & Right(aa(j), x - Len(stri))
    n = n + 1
    Next j
    m = m + 1
    Next i
    For i = 0 To 2 ^ x - 1
    Print #2, aa(i)
    Next i
    Loop
    Close #2
    Close #1
    End Sub

    BY 阿揚

    回覆刪除
  2. Dim StrA As String
    Public Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    While Not EOF(1)
    Input #1, N
    ReDim A(N) As Integer
    StrA = ""
    For i = 1 To N
    A(i) = 0
    StrA = StrA & A(i)
    Next i
    Print #2, StrA
    For i = 1 To 2 ^ N - 1
    StrA = ""
    A(1) = A(1) + 1
    For k = 1 To N - 1
    If A(k) > 1 Then A(k) = A(k) - 2: A(k + 1) = A(k + 1) + 1
    Next k
    For j = N To 1 Step -1
    StrA = StrA & A(j)
    Next j
    Print #2, StrA
    Next i
    Wend
    Close #2
    Close #1
    End
    End Sub


    BY 小白

    回覆刪除
  3. Dim aa(1000000) As String
    Dim stri As String
    Public Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, x
    Call check(x, 2)
    Loop
    Close #2
    Close #1
    End Sub

    Public Function check(x, num) As String
    For i = 0 To num ^ x - 1
    check = ""
    k = i
    Do Until k = 0
    step = k Mod num
    k = k \ num
    check = step & check
    Loop
    If Len(check) <> x Then check = String(x - Len(check), "0") & check
    Print #2, check
    Next i

    End Function


    BY 阿揚

    回覆刪除
  4. Dim aa(1000000) As String
    Dim stri As String
    Public Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Me.Hide
    Do Until EOF(1)
    Input #1, x
    Call check(x, 2, 2)
    Loop
    Close #2
    Close #1
    End
    End Sub

    Public Function check(x, num, stp) As String
    For i = 0 To num - 1
    aa(i) = String(x - 1, "0") & i
    Print #2, aa(i)
    Next i
    n = i
    k = 10
    Do Until n = num ^ x
    For i = 0 To stp - 1
    aa(n) = k + Val(aa(i))
    aa(n) = String(x - Len(aa(n)), "0") & aa(n)
    Print #2, aa(n)
    n = n + 1
    Next i
    stp = n
    k = k * 10
    Loop
    End Function








    阿揚

    回覆刪除
  5. 兩個的function意義錯誤。
    function是會傳回值的。
    sub是一段副程式,不用傳回東西的。
    這題照我的想法是用 function寫沒有錯,但是你們的程式,
    是拿function來做sub的事。

    回覆刪除
  6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.Items.Clear()
    Dim count As Integer = TextBox1.Text '把輸入的bit個數放入count
    Dim final As String '存放解的變數
    length = count '把bit個數存入length 相當為長度
    Call com(count, final)
    End Sub

    Sub com(ByVal a, ByRef final)
    For i As Integer = 0 To 1
    If (a >= 0) Then
    Call com(a - 1, final & i.ToString) '這是一個遞迴式,因為每一次都是在做同樣的事情(產生0跟1做排序),我用變數a來控制我呼叫的次數,然後把目前串連的值傳到下一層
    End If

    '判斷長度是否為當初輸入的 是則為解 加入到List後把當下的解清空
    If (Len(final) = length) Then
    ListBox1.Items.Add(final)
    final = ""
    End If
    Next
    End Sub

    回覆刪除