階層運算是很令人頭疼的,因此我们要想方設法地把它簡化。
輸入說明 :
输入檔可能有大量的數據。
每一个输入檔输入一个不算很大的數 n (请用 long)。
每一个输入檔输入一个不算很大的數 n (请用 long)。
輸出說明 :
輸出 n!的末尾零的個數。
範例輸入 :
1
2
10
範例輸出 :
0
0
2
提示 :
1!=1 末尾有0個零;
2!=2 末尾有0個零;
10!=3628800 末尾有2個零。
2!=2 末尾有0個零;
10!=3628800 末尾有2個零。
Dim countt, num(999), ans(999) As Integer
回覆刪除Private Sub Form_Load()
countt = 0
Open App.Path & "\in.txt" For Input As #1
Do
Input #1, num(countt)
countt = countt + 1
Loop Until EOF(1)
countt = countt - 1
Close #1
Sum = 1
For i = 0 To countt
For ii = 1 To num(i)
Sum = Sum * ii
Next
num(i) = Sum
Sum = 1
Next
For aii = 0 To countt
For aiii = Len(num(aii)) To 1 Step -1
If Mid(num(aii), aiii, 1) <> "0" Then Exit For
If Mid(num(aii), aiii, 1) = "0" Then ans(aii) = ans(aii) + 1
Next
Next
Open App.Path & "\out.txt" For Output As #1
For ci = 0 To countt
Print #1, ans(ci)
Next
Close #1
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do Until EOF(1)
Input #1, n
n = Val(n)
Dim a As String
a = 1
For i = 1 To n
a = i * a
Next
q = Len(a)
k = 0
w = 0
Do
p = Mid(a, q - k, 1)
k = k + 1
'MsgBox p
If p = 0 Then
w = w + 1
Else
Exit Do
End If
Loop
Print #2, w
Loop
Close #2
Close #1
End
End Sub
輸入32767兩個都溢位,試著把 X!寫出來看有什麼特性。
回覆刪除Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
numberr = 20
Do
Input #1, num
If num < 5 Then
Print #2, 0
Else
For i = 6 To num
numberr = numberr * i
For ii = Len(numberr) To 1 Step -1
If Mid(numberr, ii, 1) <> 0 Then numberr = Right(numberr, (Len(numberr) + 1) - ii): Exit For
Next
Next
ori = Len(numberr)
numberr = Replace(numberr, "0", "")
Print #2, ori - Len(numberr)
End If
Loop Until EOF(1)
Close #2
Close #1
End
End Sub
還是有錯喔
回覆刪除給你正確的看看
in out
1 0
2 0
3 0
4 0
5 1
6 1
7 1
8 1
9 1
10 2
11 2
12 2
13 2
14 2
15 3
16 3
17 3
18 3
19 3
20 4
21 4
22 4
23 4
24 4
25 6
26 6
27 6
28 6
29 6
30 7
31 7
32 7
33 7
34 7
35 8
36 8
37 8
38 8
39 8
40 9
41 9
42 9
43 9
44 9
45 10
46 10
47 10
48 10
49 10
50 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
numberr = 120
Do
Input #1, num
If num < 5 Then
Print #2, 0
Else
For i = 6 To num
numberr = numberr * i
For ii = Len(numberr) To 1 Step -1
If Mid(numberr, ii, 1) <> 0 Then numberr = Right(numberr, (Len(numberr) + 1) - ii): Exit For
Next
Next
ori = Len(numberr)
numberr = Replace(numberr, "0", "")
Print #2, ori - Len(numberr)
End If
Loop Until EOF(1)
Close #2
Close #1
End
End Sub
Dim numberr As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Do
Input #1, num
If num < 5 Then
Print #2, 0
Else
For i = 5 To num
temp = i
Do
If temp Mod 5 <> 0 Then Exit Do
If temp Mod 5 = 0 Then ans = ans + 1: temp = temp \ 5
Loop Until temp = 1
Next
End If
Print #2, ans: ans = 0
Loop Until EOF(1)
Close #2
Close #1
End
End Sub
作者已經移除這則留言。
回覆刪除Dim x As String
回覆刪除Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As 2
Do
Input #1, x
If x < 5 Then
Print #2, 0
For i = 1 To x
temp = i
Do
If temp <> 0 Then Exit Do
If temp = 0 Then ans = ans + 1: temp = temp \ 5
Loop Until temp = 1
Next
End If
Print #2, ans: ans = 0
Loop Until EOF(1)
Close
Close
End
End Sub