2012年11月5日 星期一

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

從指定目錄"in.txt"讀取一數字N,求N!的結果數值中,從右邊算來第一個不是0的數字,輸出至指定目錄"out.txt"。
( 0 < N < 10000 )

輸入範例:10

輸出範例:8

8 則留言:

  1. Dim txt As Long
    Dim ans As String

    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Input #1, txt
    Close #1

    Sum = 1

    If txt > 0 And txt < 10000 Then

    For i = 1 To txt
    Sum = Sum * i

    For ii = 0 To Len(Sum) - 1
    If Not Mid(Sum, Len(Sum) - ii, 1) = "0" Then
    ans = Mid(Sum, Len(Sum) - ii, 1)
    Sum = ans
    Exit For
    End If
    Next


    Next
    End If


    For ii = 0 To Len(Sum) - 1
    If Not Mid(Sum, Len(Sum) - ii, 1) = "0" Then
    ans = Mid(Sum, Len(Sum) - ii, 1)
    Exit For
    End If
    Next

    MsgBox Sum

    Open App.Path & "\out.txt" For Output As #2
    Print #2, ans
    Close #2

    End Sub

    回覆刪除
  2. 哈囉:D
    以後程式裡如果有自己測試輸出的程式碼的話,再PO答案之前要先註解調喔
    ex:'MsgBox Sum

    回覆刪除
  3. 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 a, n, i As Long
    Input #1, n
    n = Val(n)
    a = 1
    i = 0
    For i = 1 To n
    a = a * i
    Next
    'MsgBox a
    j = Len(a)
    i = 0
    For i = j - 1 To 0 Step -1
    q = Mid(a, i, 1)
    If Val(q) <> 0 Then Exit For
    Next
    Print #2, q
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 復陞好,輸入檔為9999時會溢位,想辦法除錯看看:)

    回覆刪除
  5. 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
    b = 1
    For i = n To 1 Step -1

    If b > 10000 Then b = b Mod 10000
    b = b * i
    Do While b Mod 10 = 0 And b \ 10 > 1
    b = b \ 10
    Loop

    Next
    Print #2, Right(b, 1)
    Close
    Close
    End
    End Sub

    回覆刪除
  6. 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 a, n, i As Long
    Input #1, n
    n = Val(n)
    a = 1
    For i = 1 To n
    a = a * i
    s = Len(a)
    For j = s To 1 Step -1
    p = Mid(a, j, 1)
    If p <> 0 Then
    a = Mid(a, j, 1)
    Exit For
    End If
    Next
    Next
    Print #2, a
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  7. Private Sub Form_Load()
    Me.Hide
    Open "in.txt" For Input As #1
    Open "out.txt" For Output As #2
    Input #1, a
    k = 1
    For i = 2 To a
    k = k * i
    Do
    c = k Mod 10
    k = k \ 10
    Loop Until c <> 0
    k = c
    Next i
    Print #2, k
    End
    End Sub

    回覆刪除