2011年2月13日 星期日

2010第一次儲備選手選拔試卷第五題

數字系統轉換:從指定目錄"in.txt"讀取一正整數,將它轉為16進位後輸出至指定目錄"out.txt"。
(不可使用內定含數轉換)

輸入範例:168

輸出範例:A8

10 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, x

    Y = x Mod 16

    If Y > 9 Then
    Select Case Y
    Case 10: Y = "A"
    Case 11: Y = "B"
    Case 12: Y = "C"
    Case 13: Y = "D"
    Case 14: Y = "E"
    Case 15: Y = "F"
    End Select
    End If

    z = x \ 16

    If z > 9 Then
    Select Case z
    Case 10: z = "A"
    Case 11: z = "B"
    Case 12: z = "C"
    Case 13: z = "D"
    Case 14: z = "E"
    Case 15: z = "F"
    End Select
    End If
    Print #2, z & Y

    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  2. 程式有錯噢

    你試試 15 結果要 F 你的是 0F
    300 結果要 12C 你的是 18C

    //----------

    可以配合 Hex() 這個函數來確認是不是對的

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Input #1, x

    Do While x >= 1

    Y = x Mod 16

    If Y > 9 Then
    Select Case Y
    Case 10: Y = "A"
    Case 11: Y = "B"
    Case 12: Y = "C"
    Case 13: Y = "D"
    Case 14: Y = "E"
    Case 15: Y = "F"
    End Select
    End If
    a = Y & a
    x = x \ 16
    Loop
    Print #2, a

    Close #1
    Close #2
    End
    End Sub
    -------------
    好多錯喔... 謝謝Arro~
    在寫程式的時候就一直在想要怎麼讓他能夠重複而不是只判斷兩位數...也許我還不是很活用迴圈>"<
    (努力ING)

    回覆刪除
  4. 緣尉好,
    這題這樣改了,正確了。
    但是,以後還是記得將初始值給寫上去,a=""
    arro好,
    你怎麼只是喊了加油,沒自己寫一寫呢?

    回覆刪除
  5. Dim A As Integer '十進位數
    Dim B As Integer '十進位數16整除
    Dim C As Integer '十進位數16除餘數
    Dim D As String '10~16的英文字
    Dim E As String '十六進位數
    Private Sub Form_Load()
    Me.Hide

    Open App.Path & "\in.txt" For Input As #1
    Input #1, A
    Close #1

    B = A \ 16
    C = A Mod 16
    E = ""

    If B = 0 Then
    E = E & "0"
    If C >= 10 Then Call ten1(C): E = E & D Else E = E & C
    Else
    If B >= 10 Then Call ten2(B): E = E & D Else E = E & B
    If C >= 10 Then Call ten1(C): E = E & D Else E = E & C
    End If

    Open App.Path & "\out.txt" For Output As #2
    Print #2, E
    Close #2

    End
    End Sub
    Sub ten1(C)
    Select Case C
    Case 10: D = "A"
    Case 11: D = "B"
    Case 12: D = "C"
    Case 13: D = "D"
    Case 14: D = "E"
    Case 15: D = "F"
    End Select
    End Sub
    Sub ten2(B)
    Select Case B
    Case 10: D = "A"
    Case 11: D = "B"
    Case 12: D = "C"
    Case 13: D = "D"
    Case 14: D = "E"
    Case 15: D = "F"
    End Select
    End Sub

    不太懂規則,
    還特地去複習了一下。

    回覆刪除
  6. 佑好,
    你和緣尉第一次寫的程式一樣的錯誤,以為結果只有兩位數。
    超過兩位數的呢?

    回覆刪除
  7. 熊掌好...

    很高興的終於把這題搞定

    Dim A As Integer '十進位數
    Dim A2 As Integer '計算用
    Dim B As Integer '次方數
    Dim D As String '10~16的英文字
    Dim E As String '十六進位數
    Private Sub Form_Load()
    Me.Hide

    Open App.Path & "\in.txt" For Input As #1
    Input #1, A
    Close #1

    D = ""

    If A \ 16 <> 0 Then
    Do Until (16 ^ B) > A
    B = B + 1
    Loop
    B = B - 1
    End If

    For i = B To 0 Step -1
    A2 = A \ (16 ^ i)
    If A \ 16 ^ i = 0 Then
    E = E & "0"
    Else
    Call ten(A2)
    E = E & D
    A = A - (16 ^ i * A2)
    End If
    Next i


    Open App.Path & "\out.txt" For Output As #2
    Print #2, E
    Close #2

    End
    End Sub
    Sub ten(A2)
    Select Case A2
    Case 1 To 9: D = A2
    Case 10: D = "A"
    Case 11: D = "B"
    Case 12: D = "C"
    Case 13: D = "D"
    Case 14: D = "E"
    Case 15: D = "F"
    End Select
    End Sub

    今天一整天都在想。
    至今花最久的一題。

    回覆刪除
  8. 回頭看看緣尉的程式碼
    發現原來他是這樣想過來
    而我是這樣想過去=口=

    我的方法是先找出
    十進位數的16最高次然後減下來

    回覆刪除
  9. 佑好,
    應該正確了。
    但是,那個B,也是沒必要的。又是多減了一,又是從高到低的再一次迴圈。
    還有,你先算了
    A2 = A \ (16 ^ i)
    If A \ 16 ^ i = 0 Then
    應該用 if a2 = 0 then 可以減少一次複雜運算的。
    不過,寫著寫著,前進中,很好,加油。

    回覆刪除
  10. 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
    Call A1(n)
    Close
    Close
    End
    End Sub

    Sub A1(a)
    Dim s, ans
    s = Split("0 1 2 3 4 5 6 7 8 9 A B C D E F")

    Do
    ans = s(a Mod 16) & ans
    a = a \ 16
    Loop Until a = 0

    Print #2, ans
    End Sub


    2:45

    回覆刪除