2012年5月18日 星期五

Find the Telephone


內容 : 
有些地方會用對應的字母來代替數字使得電話號碼更好記。如此一來 MY LOVE 就代表 69 5683。這不是萬靈丹,因為有的電話號碼並不能構成一個字或片語,而且 1 和 0 沒有對應的字母。
請讀入一個字串並依據下表轉成電話號碼。字串由大寫字母 (A-Z)、連字號(-) 和數字 1 和 0 所組成。

字母數字
ABC2
DEF3
GHI4
JKL5
MNO6
PQRS7
TUV8
WXYZ9
輸入說明 :
輸入含有若干字串。每個字串單獨在一行,有 C 個字元,1 ≤ C ≤ 30 。輸入以 EOF 作為結束。
輸出說明 :
對於每個字串,請輸出相對應的電話號碼。
範例輸入 :help
1-HOME-SWEET-HOME 
MY-MISERABLE-JOB
範例輸出 :
1-4663-79338-4663 
69-647372253-562

4 則留言:

  1. 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 While Not EOF(1)
    Line Input #1, n
    For i = 1 To Len(n)
    Select Case Mid(n, i, 1)
    Case "A" To "C"
    ans = ans & 2
    Case "D" To "F"
    ans = ans & 3
    Case "G" To "I"
    ans = ans & 4
    Case "J" To "L"
    ans = ans & 5
    Case "M" To "O"
    ans = ans & 6
    Case "P" To "S"
    ans = ans & 7
    Case "T" To "V"
    ans = ans & 8
    Case "W" To "Z"
    ans = ans & 9
    Case Else
    ans = ans & Mid(n, i, 1)
    End Select
    Next
    Print #2, ans
    ans = ""
    Loop
    Close
    Close
    End
    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
    Dim ans, XX As String
    Do While Not EOF(1)
    ans = " "
    Line Input #1, N
    For I = 1 To Len(N)
    XX = Mid(N, I, 1)
    Select Case XX
    Case "A" To "C"
    XX = 2
    Case "D" To "F"
    XX = 3
    Case "G" To "I"
    XX = 4
    Case "J" To "L"
    XX = 5
    Case "M" To "O"
    XX = 6
    Case "P" To "S"
    XX = 7
    Case "T" To "V"
    XX = 8
    Case "W" To "Z"
    XX = 9
    End Select
    ans = ans & XX
    Next I
    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Dim ans As String
    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 While Not EOF(1)
    Line Input #1, x
    strA = ""
    arr = Split(x, "-")
    For i = 0 To UBound(arr)
    For j = 1 To Len(arr(i))
    y = a(Mid(arr(i), j, 1))
    strA = strA & y
    Next j
    If i <> UBound(arr) Then strA = strA & "-"
    Next i
    Print #2, strA
    Loop
    Close #2
    Close #1
    End
    End Sub

    Function a(b)
    Select Case b
    Case "A" To "C"
    ans = 2
    Case "D" To "F"
    ans = 3
    Case "G" To "I"
    ans = 4
    Case "J" To "L"
    ans = 5
    Case "M" To "O"
    ans = 6
    Case "P" To "S"
    ans = 7
    Case "T" To "V"
    ans = 8
    Case "W" To "Z"
    ans = 9
    Case Else
    ans = b
    End Select
    a = ans
    End Function

    回覆刪除