2011年1月10日 星期一

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

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

12 則留言:

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

    Input #1, x, y

    a = x * y
    m = Len(a)

    For i = 1 To m
    n = Mid(a, i, 1)
    k(n) = k(n) + 1
    Next i

    For i = 0 To 9
    If k(i) = 0 Then
    Print #2, "";
    Else
    Print #2, i & k(i) & " ";
    End If
    Next i

    Close #1
    Close #2
    End
    End Sub

    回覆刪除
  2. 感覺你的 Print #2, ""; 這行是多餘的

    回覆刪除
  3. 緣尉好,
    1.程式OK。
    2.我正想說的話,arro說了。(謝謝你,arro,會看看別人的程式,找出優缺點,也是很重要的哦)
    熊掌

    回覆刪除
  4. 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, y

    ins = x * y
    Dim s(9) As Integer

    For i = 0 To 9
    For j = 1 To Len(ins)
    k = Mid(ins, j, 1)
    If k = Val(i) Then s(i) = s(i) + 1
    Next
    Next

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

    Close #2
    Close #1
    End
    End Sub


    ------
    隨身碟找到了 =D

    回覆刪除
  5. Arro好,
    程式乍看可以,但是容易出錯,又太重複。
    數字變數的長度len(ins)有定義跟沒定義會有不同。
    找一下去年的討論。
    數字其實還是適合用「取餘數」和「整除」來做。
    熊掌ipad

    回覆刪除
  6. Dim N As String
    Dim A1 As Long
    Dim A2 As Long
    Dim A3 As String
    Dim A(10) As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Input #1, N
    Close #1
    A1 = Val(Left(N, 4))
    A2 = Val(Right(N, 4))
    A3 = Str(A1 * A2)

    Do Until B = Len(A3)
    B = B + 1
    A(InStr("0123456789", Mid(A3, B, 1))) = A(InStr("0123456789", Mid(A3, B, 1))) + 1
    Loop

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

    回覆刪除
  7. 佑好,
    程式可以,那主要的一行,看起來太複雜,不過也可以啦。
    熊掌

    回覆刪除
  8. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim stor(10): Dim chk(10): Dim ans1(10)

    Input #1, a, b


    For ci = Len(a) To 1 Step -1
    i = i + 1
    For cj = Len(b) To 1 Step -1
    j = j + 1
    stor(i + j - 1) = stor(i + j - 1) + Mid(a, ci, 1) * Mid(b, cj, 1): chk(i + j - 1) = True
    If stor(i + j - 1) > 9 Then
    stor(i + j) = stor(i + j) + Val(Left(stor(i + j - 1), 1))
    stor(i + j - 1) = Val(Right(stor(i + j - 1), 1))
    If stor(i + j) > 9 Then
    stor(i + j + 1) = stor(i + j + 1) + Val(Left(stor(i + j - 1), 1))
    stor(i + j) = Val(Right(stor(i + j), 1))
    chk(i + j + 1) = True
    End If
    chk(i + j) = True
    End If
    Next cj
    j = 0
    Next ci
    i = 0

    For i = 0 To 10
    If chk(i) = True Then ans1(stor(i)) = ans1(stor(i)) + 1
    Next i



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

    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  9. Jimmer好,
    程式正確。
    但是,你想做的是「大數運算」吧?
    比賽時,是有時間限制的。
    用最少的力氣去做。
    熊掌

    回覆刪除
  10. 就想說現在有空研究一下
    當時進位那邊一直出錯
    頭腦整個有點混亂掉...

    回覆刪除
  11. Jimmer好,
    1+9999999要進位幾次?
    取進位的方式用「\ 和mod」比較好,
    大數加法先試試吧
    熊掌

    回覆刪除
  12. 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, a, b
    Call A1(a, b)
    Close
    Close
    End
    End Sub

    Sub A1(a, b)
    Dim c, s(9) As Integer
    c = a * b
    For i = 1 To Len(c)
    m = Mid(c, i, 1)
    s(m) = s(m) + 1
    Next
    For i = 0 To 9
    If s(i) <> 0 Then Print #2, i & s(i) & " ";
    Next
    End Sub

    2: 06

    回覆刪除