Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Input #1, a Close #1 b = 1 For i = 2 To a b = b * i For j = Len(CStr(b)) To 1 Step -1 If Mid(b, j, 1) <> 0 Then b = Mid(b, j, 1): Exit For Next j Next i
Open App.Path & "\out.txt" For Output As #2 Print #2, b Close #2
佑好, >Do > If b Mod 10 = 0 Then b = b \ 10 Else b = Right(b, 1) > Loop Until (b Mod 10 <> 0) >前面又多了個忘記的c 1. b的值怪怪的,例如,當b為330好了, 由於b mod 10=0 為真,所以b=b\10, b是33,到了loop的條件,33 mod 10 <> 0 為真,所以出來了,那麼,b得到的是33並不是原先期待的值哦。 雖然,也會將結果做出來。 2. 然後,b是數字,前面用b mod 10 , 後面怎麼又用 b = right(b,1) 直接b=b mod 10就好了。
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 While Not EOF(1) Input #1, n If n <> 0 Then Call A1(n) Loop Close Close End End Sub
Sub A1(a) k = 1
For i = 1 To a k = k * i Do While Right(k, 1) = 0 k = k / 10 Loop k = Right(Trim(k), 5) Next Print #2, Right(k, 1) End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, n
m = 1
For i = 2 To n
m = m * i
y = Mid(m, Len(m), 1)
If y <> "0" Then
a = y
Else
m = Mid(m, Len(m) - 1, 1)
End If
Next
Print #2, a
Close #1
Close #2
End
End Sub
你的程式有錯噢 @@
回覆刪除( 0 < N < 10000 )
n= 9999 會出錯呢~
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, n
m = 1
For i = 2 To n
m = m * i
y = Mid(m, Len(m), 1)
If y <> "0" Then
a = y
Exit For
Else
m = Mid(m, Len(m) - 1, 1)
End If
Next
Print #2, a
Close #1
Close #2
End
End Sub
-------------------
謝謝指正=]
找好久="=
緣尉好,
回覆刪除這樣子,應該還是不對吧。
用筆算算幾個數,然後你的程式去執行看看。
**
mod 10
\10
多利用這兩個運算,一個是取個位數,一個是去掉個位數。
因為一次出現的0,可能超過一個。
Dim A As Integer
回覆刪除Dim B As Double
Dim ans As Byte
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, A
Close #1
B = 1
For i = 1 To A
B = B * i
Next i
For i = Len(CStr(B)) To 1 Step -1
If Mid(B, i, 1) <> 0 Then ans = Mid(B, i, 1): Exit For
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, ans
Close #2
End
End Sub
這樣n=9999會溢位
要算到9999!很難吧
難道要邊算邊判斷
還是!!?
佑好,
回覆刪除->難道要邊算邊判斷
是的。
而且,反正題目要的只是第一個不是0的數,先用筆算算,你會發現很簡單的。
Dim a As Integer
回覆刪除Dim b As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, a
Close #1
b = 1
For i = 2 To a
b = b * i
For j = Len(CStr(b)) To 1 Step -1
If Mid(b, j, 1) <> 0 Then b = Mid(b, j, 1): Exit For
Next j
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, b
Close #2
End
End Sub
佑好,
回覆刪除程式正確。
但是,想想兩種迴圈的區別:
計次迴圈-for next
條件迴圈-do loop
像這題中的找出第一個非0的數,明明就是條件迴圈,試試吧。
熊掌好,
回覆刪除了解
所以
for next 已知次數的迴圈,
未知次數的迴圈才用do loop
Dim a As Integer
Dim b As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, a
Close #1
b = 1
For i = 2 To a
b = b * i
c = Len(CStr(b))
Do
If b Mod 10 = 0 Then b = b \ 10 Else b = Right(b, 1)
Loop Until (b Mod 10 <> 0)
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, b
Close #2
End
End Sub
寫了個後側式判斷
讓
If b Mod 10 = 0 Then b = b \ 10 Else b = Right(b, 1)
這行跑得到:D
佑好,
回覆刪除>Do
> If b Mod 10 = 0 Then b = b \ 10 Else b = Right(b, 1)
> Loop Until (b Mod 10 <> 0)
>前面又多了個忘記的c
1. b的值怪怪的,例如,當b為330好了,
由於b mod 10=0 為真,所以b=b\10, b是33,到了loop的條件,33 mod 10 <> 0 為真,所以出來了,那麼,b得到的是33並不是原先期待的值哦。
雖然,也會將結果做出來。
2. 然後,b是數字,前面用b mod 10 , 後面怎麼又用 b = right(b,1)
直接b=b mod 10就好了。
佑好,
回覆刪除忘了寫上我的版本。
do until b mod 10 <> 0
b = b \10
loop
b = b mod 10
熊掌好,
回覆刪除C忘記除掉
1.
我如果用Cint(Right(Cint(b),1))只會取到1位
不會拿到十位數之後
缺點是要形態轉換麻煩
那個問題是用b =b mod 10 就會出現
loop until B<10 這麼一來就是一個數字才離開摟
2.b = b mod 10
不用再把b轉成字串又轉回去。
了解。
謝謝
Dim a As Integer
Dim b As Integer
Private Sub Form_Load()
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Input #1, a
Close #1
b = 1
For i = 2 To a
b = b * i
Do
If b Mod 10 = 0 Then b = b \ 10 Else b = b mod 10
Loop Until (b < 10)
Next i
Open App.Path & "\out.txt" For Output As #2
Print #2, b
Close #2
End
End Sub
熊掌好,
回覆刪除回完才看到
do until b mod 10 <> 0
b = b \10
loop
b = b mod 10
出來後又給他補一刀:D
了解~謝謝
看完 佑 的程式後
回覆刪除發現原來不難寫
不過蠻有趣的
直接
Print b 會溢位
而 b = b mod 10 則沒有問題
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, n
m = 1
For i = 2 To n
m = m * i
Do
y = m Mod 10
m = m \ 10
Loop Until y <> 0
m = y
Next
Print #2, m
Next n
Close #1
Close #2
End
End Sub
緣尉好,
回覆刪除程式正確。
Do
y = m Mod 10
m = m \ 10
Loop Until y <> 0
m = y
-->
y = m mod 10
do while y = 0
m = m \10
y = m mod 10
loop
m = y
哦,算了,本來想說,用後面一種方式會好些,寫了之後,發現沒有更好,好像還更多行了。
開始利用早自習的半小時吧,試試,加油。
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 While Not EOF(1)
Input #1, n
If n <> 0 Then Call A1(n)
Loop
Close
Close
End
End Sub
Sub A1(a)
k = 1
For i = 1 To a
k = k * i
Do While Right(k, 1) = 0
k = k / 10
Loop
k = Right(Trim(k), 5)
Next
Print #2, Right(k, 1)
End Sub
3:29