2012年11月5日 星期一

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


計算二數成績的數字個數:從指定目錄"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

3 則留言:

  1. Dim num1, num2 As Integer
    Dim sum As Long
    Dim countt(9) As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, num1
    Input #1, num2
    Close #1

    sum = num1 * num2

    For i = 1 To Len(Str(sum)) - 1
    ok = Mid(sum, i, 1)
    countt(ok) = countt(ok) + 1
    Next

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

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Dim b(9)
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x, y
    a = x * y
    For i = 0 To 9
    For j = 1 To Len(a)
    c = Val(Mid(a, j, 1))

    If c = i Then b(i) = b(i) + 1
    Next
    If b(i) <> 0 Then Print #2, i & b(i) & " ";
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Dim c(9) As Long
    Dim z As Long
    For i = 0 To 9
    c(i) = 0
    Next
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Input #1, y
    z = x * y
    w = Len(Str((z)))
    For i = 1 To w - 1
    num = Mid(z, i, 1)
    c(num) = c(num) + 1
    Next
    Print #2, z
    For i = 0 To 9
    Print #2, i; c(i)
    Next
    Close
    Close
    End Sub

    回覆刪除