2012年11月20日 星期二

質因數分解

從輸入檔in.txt中,讀入1正的長整數,請將此數分解成質因數的乘積,輸出到out4.txt

輸入範例: in.txt
248417
輸出範例: out.txt
248417 = 13 * 97 * 197

2 則留言:

  1. Dim yes As Boolean
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    realans = 1
    Input #1, num
    ans = num & "="
    For i = 2 To num
    For ii = 1 To i
    If i Mod ii = 0 Then mycount = mycount + 1
    Next
    If mycount = 2 Then yes = True
    If num Mod i = 0 And yes = True Then ans = ans & i & "*": realans = realans * i
    mycount = 0: yes = False
    If realans = num Then Exit For
    Next
    Print #2, Left(ans, Len(ans) - 1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim msg As String
    Dim tof As Boolean
    Private Sub Form_Load()
    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, x
    msg = x & " = ": cof = True
    Call a(Val(x))
    Loop
    Close #2
    Close #1
    End
    End Sub

    Sub a(b)
    If b = 1 Then Print #2, Left(msg, Len(msg) - 2): End
    For i = 2 To b
    tof = True
    For j = 2 To i - 1
    If i <> j Then
    If i Mod j = 0 Then tof = False
    End If
    Next j
    If tof = True And b Mod i = 0 Then b = b / i: msg = msg & i & " * ": Call a(b)
    Next i
    End Sub

    回覆刪除