2012年11月5日 星期一

計算位元為1 的個數

計算機概論是一門令人又愛又恨的科目,它的內容可謂包羅萬象。遇到考試時,事前需
要花很多時間準備,才能拿到高分。在學習的內容中,有個章節是數字系統轉換,內容是將
一個十進位的數字,轉換成二進位的數字。現在請你設計一個程式,計算由十進位數字轉換
的二進位數字中,位元等於1 的個數。

輸入說明:
第一行的數字,代表有幾個十進位的數字。第二行開始的每一行,為一個十進位數字,
其範圍為[0, 2147483647]的整數。

輸出說明:
對輸入的十進位數字,以一行分別輸出轉換成二位進數字中,位元等於1 的個數。

輸入範例:
2
1027
65535

輸出範例:
3
16

7 則留言:

  1. Dim countt, num(99), onecount(99) As Long
    Dim twostr(99) As String
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, countt
    For i = 0 To countt - 1
    Input #1, num(i)
    Next
    Close #1


    For ai = 0 To 99
    If num(ai) <> "0" Then
    Do
    twostr(ai) = Str(num(ai) Mod 2) & twostr(ai)
    num(ai) = num(ai) \ 2
    Loop Until num(ai) = 0
    End If
    Next


    For bi = 0 To countt - 1
    For iii = 1 To Len(twostr(bi))
    If Mid(twostr(bi), iii, 1) = "1" Then onecount(bi) = onecount(bi) + 1
    Next
    Next

    Open App.Path & "\out.txt" For Output As #1
    For iiii = 0 To countt - 1
    Print #1, onecount(iiii)
    Next
    Close #1
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    Input #1, x
    ans = 0
    Do
    y = x Mod 2
    x = x \ 2
    ans = ans + y
    Loop Until x = 0
    Print #2, ans
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Dim t As Boolean
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    Input #1, x
    b = 0
    Do
    a = x Mod 2
    x = x \ 2
    b = a + b
    Loop Until x = 0
    Print #2, b
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  4. Dim countt, num As Long
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As 2
    Input #1, countt
    For i = 1 To countt
    Input #1, num
    Do
    y = num Mod 2
    num = num \ 2
    ans = ans + y
    Loop Until num = 0
    Print #2, ans
    ans = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    For i = 1 To n
    Input #1, x
    ans = 0
    Do
    y = x Mod 2
    x = x \ 2
    ans = ans + y
    Loop Until x = 0
    Print #2, ans
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  6. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, times
    For i = 1 To times
    zzz = 0
    Input #1, a
    Do Until a = 1 Or 0
    b = a Mod 2
    If b = 1 Then a = a - 1
    If b = 1 Then zzz = zzz + 1
    c = Val(a) / 2
    a = Val(c)
    ans = b & ans
    Loop
    If a = 1 Then zzz = zzz + 1
    Print #2, zzz
    Next i
    Loop
    Close
    End
    End Sub

    回覆刪除