請各位高手中文解釋一下
我看不太懂:
Private Sub Command1_Click()
Dim n As Long
n = InputBox("輸入一正整數值")
msg = n & "="
For i = 2 To n
Do While n Mod i = 0
n = n / i
msg = msg & i & "x"
Loop
Next i
MsgBox Left(msg, Len(msg) - 1)
End Sub
2007-12-23 11:31:19 · 1 個解答 · 發問者 ↖小羊↘ 1 in 電腦與網際網路 ➔ 程式設計
(1)msg = n & "="
(2)For i = 2 To n
(3)Do While n Mod i = 0
(4)n = n / i
(5)msg = msg & i & "x"
(6)Loop
(7)Next i
(8)MsgBox Left(msg, Len(msg) - 1)
首先輸入n值
(1)結果以存入變數msg中
(2)由(2)至(7)檢查2~n的每一個數是否可整除n
其實for i=2 to n/2即可
(3)由(3)至(6)檢查並因數分解n, 並將因數i存入msg中
n mod i=0 表 n 除以 i 餘數=0
(4)若 n mod i =0 , 則 因數分解 n=n/i (將n改為商)
(5)因數存入msg中
(6)繼續分解
(8)輸出因數分解的結果(msg),
輸出之前msg最後一個字元為 "x" (乘號), 必須去除
例:輸入n=60, 則msg變化如下:
msg: 60=
msg: 60=2x
msg: 60=2x2x
msg: 60=2x2x3x
msg: 60=2x2x3x5x
Left(msg, Len(msg) - 1) =>取60=2x2x3x5
最後輸出
2007-12-23 14:08:24 · answer #1 · answered by mathmanliu 7 · 1⤊ 0⤋