2010年1月14日 星期四

第一次儲備選手選拔試卷第三題

計算二數成績的數字個數:從指定目錄"in.txt"讀取二個四位數正整數,相乘的積,計算該數字0到9的數字個數,輸出至指定目錄"out.txt"。
如1255*2100=2635500,結果有0有2個,2有1個,3有1個,5有2個,6有1個。其他個有0個,不用顯示。

輸入範例:1255 2100

輸出範例:02 21 31 52 61

11 則留言:

  1. 熊掌:
    open app.path & "in.txt" for input as #1
    input #1,x,y

    可以將以空白或逗號隔開的兩個數,一次讀入

    回覆刪除
  2. Private Sub Form_Activate()
    Dim xx(9) As Integer
    Open App.Path & "\in.txt" For Input As #1
    Input #1, x, y
    Close #1
    ans$ = x * y
    num = Len(ans$)
    For i = 1 To num
    X1 = Mid(ans$, i, 1)
    Select Case X1
    Case 0
    xx(0) = xx(0) + 1
    Case 1
    xx(1) = xx(1) + 1
    Case 2
    xx(2) = xx(2) + 1
    Case 3
    xx(3) = xx(3) + 1
    Case 4
    xx(4) = xx(4) + 1
    Case 5
    xx(5) = xx(5) + 1
    Case 6
    xx(6) = xx(6) + 1
    Case 7
    xx(7) = xx(7) + 1
    Case 8
    xx(8) = xx(8) + 1
    Case 9
    xx(9) = xx(9) + 1
    End Select
    Next i
    Open App.Path & "/out.txt" For Output As #1
    x = 0
    For i = 0 To 9
    If xx(i) = 0 Then
    Else
    Print #1, x;
    Print #1, xx(i) & " ";
    End If
    x = x + 1
    Next i
    Close #1
    End Sub

    回覆刪除
  3. D im A(10) As Integer '部落格中無法宣告= =
    Private Sub Form_Activate()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, x, y
    Sum = x * y
    For i = 1 To Len(Sum)
    k = Mid(Sum, i, 1)
    A(k) = A(k) + 1
    Next i
    Open App.Path & "/out.txt" For Output As #2
    For j = 0 To 9
    If A(j) <> 0 Then Print #2, j & A(j) & " ";
    Next j
    Close #2
    Close #1
    End Sub

    回覆刪除
  4. Private Sub Form_Activate()
    Dim a(9)
    Open App.Path & "\in.txt" For Input As #1
    Input #1, x, y

    ans = x * y
    For i = 1 To Len(ans)
    w = Mid(ans, i, 1)

    a(w) = a(w) + 1
    Next i
    Close #1

    Open App.Path & "\out.txt" For Output As #2
    For i = 0 To 9
    If a(i) <> 0 Then
    Print #2, i & a(i) & " ";
    End If
    Next i
    Close #2
    End Sub

    回覆刪除
  5. Y揚,高仔,皓好,
    你們三個的程式都OK,
    但是,你們都將用來處理字串的函數mid, len拿來處理數字,並不是很好的方式。
    改進的方式是該用mod 10來取出最後一個數字,\10去掉一個數字,直到沒有數字了。
    試試吧。

    回覆刪除
  6. 不曉得這樣算不算錯 輸出成直的 不過應該沒問題吧(汗

    D im a(9), b(9) As String

    Private Sub Form_Activate()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, x, y
    z = x * y
    For i = 1 To Len(z)
    c = Val(Mid(z, i, 1))
    For j = 0 To 9
    b(j) = j
    If c = j Then a(j) = Val(a(j)) + 1
    Next j
    Next i
    Close #1
    Open App.Path & "/out.txt" For Output As #2
    For i = 0 To 9
    If Val(a(i)) = 0 Then b(i) = ""
    If Val(a(i)) = 0 Then a(i) = ""
    Print #2, a(i) & b(i)
    Next i
    Close #2
    End Sub

    回覆刪除
  7. 松鼠好,
    print #2,a(i)&b(i)
    ->
    print #2,a(i)&b(i);
    就可以了。

    回覆刪除
  8. Private Sub Form_Load()

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

    Dim z(9) As Integer
    ans = x * y
    txt = "0123456789"
    a = Len(ans)

    Open App.Path & "\out.txt" For Output As #2
    For i = 1 To a
    b = ans Mod 10
    c = InStr(txt, b) - 1
    z(c) = z(c) + 1
    ans = ans \ 10
    Next i

    For i = 0 To 9
    If z(i) <> 0 Then
    Print #2, i & z(i) & " ";
    End If
    Next i
    Close #2

    End Sub

    回覆刪除
  9. 阿瑋好,
    似乎用txt是你的密技哦,不錯。
    只是
    b = ans Mod 10
    c = InStr(txt, b) - 1
    z(c) = z(c) + 1
    這三行是不是多遶了一層呢? 用b來當成c就可以了吧?
    松鼠的這個部分,也是多做了一個不必要的迴圈,再想想。

    回覆刪除
  10. 這題其實也可以用切割的方式做出來
    雖然電腦本身運算比較多
    但是程式碼簡短

    回覆刪除
  11. Dim num(9) As Integer
    Public 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, y
    ans = x * y
    For i = 1 To Len(Str(ans)) - 1
    k = Mid(ans, i, 1)
    num(Val(k)) = num(Val(k)) + 1
    Next i
    For i = 0 To 9
    If num(i) <> 0 Then Print #2, i & num(i) & " ";
    Next i
    Close #2
    Close #1
    End
    End Sub

    4分24秒

    回覆刪除