2012年11月20日 星期二

96模擬 Problem 2 (字串處理

Problem 2 (字串處理 10%)
輸入一段字串(String),請寫出一個程式,計算此字串中阿拉伯數的字元有幾個?

輸入說明:第一列為要計算的列數,第二列及以後就是需要計算的字串。(請參照輸入範例)
輸入範例:test2.txt

a9sj2k13ckdi7
ba429gcbgq1

輸出說明:對每一測試資料,輸出字串中阿拉伯數字的個數。(請參照輸出範例)
輸出範例:result2.txt
5
4

4 則留言:

  1. Dim strr As String
    Dim ans As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\test2.txt" For Input As #1
    Open App.Path & "\result2.txt" For Output As #2
    Input #1, strr
    Do
    ans = 0
    Line Input #1, strr
    For i = 1 To Len(strr)
    If Val(Mid(strr, i, 1)) <> 0 Then ans = ans + 1
    Next
    Print #2, ans
    Loop Until EOF(1)
    Close #2
    Close #1
    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
    Input #1, x
    For i = 1 To x
    Input #1, n
    y = 0
    For k = 1 To Len(n)
    If Val(Mid(n, k, 1)) <> 0 Then y = y + 1
    Next
    Print #2, y
    Next
    End
    Close
    Close
    End Sub

    回覆刪除
  3. 題目輸入沒有0的阿拉伯數字串,那<>0 ANS = ANS + 1 是可行的,但如果題目出現阿拉伯數字0呢?

    回覆刪除
  4. Dim num As Integer
    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 j = 1 To n
    Line Input #1, w
    lon = Len(w)
    For i = 1 To lon
    X = Asc(Mid(w, i, 1))
    If X > 47 And X < 58 Then num = num + 1
    Next
    Print #2, num
    num = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除